glquic

check vulnerable nixpkgs gleam hex

QUIC for Gleam, built on erlang_quic

using

gleam add glquic

erlang_quic is written in Erlang, so nothing native is built. It needs Erlang/OTP 26 or later

each connection sends its events, including those of all its streams, as messages to the process that owns it: the one that connected, or the one a listener’s handler returned

client

import glquic

pub fn main() {
  let options = glquic.client_options(alpn: ["echo"])
  let assert Ok(connection) =
    glquic.connect(to: "localhost", on: 4433, with: options, timeout: 5000)

  let assert Ok(stream) = glquic.open_stream(connection)
  let assert Ok(Nil) = glquic.send_and_finish(stream, <<"Hello, Joe!">>)

  case glquic.receive(within: 5000) {
    Ok(glquic.Data(data:, ..)) -> echo data
    _ -> panic as "no reply"
  }
}

server

import gleam/erlang/process
import glquic

pub fn main() {
  let options =
    glquic.server_options(
      alpn: ["echo"],
      certificate: "cert.pem",
      private_key: "key.pem",
    )
  let assert Ok(_listener) =
    glquic.listen(on: 4433, with: options, handler: fn(_connection) {
      process.spawn(echo_loop)
    })
  process.sleep_forever()
}

fn echo_loop() -> Nil {
  case glquic.receive(within: 60_000) {
    Ok(glquic.Data(stream:, data:, fin: True)) -> {
      let _ = glquic.send_and_finish(stream, data)
      echo_loop()
    }
    Ok(glquic.Data(stream:, data:, fin: False)) -> {
      let _ = glquic.send(stream, data)
      echo_loop()
    }
    Ok(glquic.ConnectionClosed(..)) | Error(Nil) -> Nil
    Ok(_) -> echo_loop()
  }
}

events can also be added to a selector with glquic.select, for use in actors

contributing

see CONTRIBUTING.md for requirements and getting started

✨ Search Document