Files
nim-quic/README.md
2025-09-25 14:42:26 +00:00

1.6 KiB

QUIC for Nim

An implementation of the QUIC protocol in Nim.

Building and testing

Install dependencies:

nimble install -d

Run tests:

nimble test

Examples

Import quic and the chronos async library:

import quic
import chronos

Create server:

let tlsConfig = TLSConfig.init(cert, certPrivateKey, @["alpn"], Opt.none(CertificateVerifier))
let server = QuicServer.init(tlsConfig)

Accept incoming connections:

let address = initTAddress("127.0.0.1:12345")
let listener = server.listen(address)
defer:
  await listener.stop()
  listener.destroy()

while true:
  let connection =
    try:
      await listener.accept()
    except CatchableError:
      return # server stopped
  let stream = await connection.incomingStream()
  
  # read or write data to stream
  let data = await stream.read()

  await stream.close()
  await connection.waitClosed()

Create client:

let tlsConfig = TLSConfig.init(cert, certPrivateKey, @["alpn"], Opt.none(CertificateVerifier))
let client = QuicClient.init(tlsConfig)

Dial server and send message:

let address = initTAddress("127.0.0.1:12345")
let connection = await client.dial(address)
defer:
  await connection.close()

let stream = await connection.openStream()
let message = cast[seq[byte]]("hello form nim quic client")
await stream.write(message)
await stream.close()

Thanks

We would like to thank the authors of the NGTCP2 library, on whose work we're building.