Skip to content

beryl: real-time channels for Gleam

Type-safe real-time channels and presence for Gleam, targeting the Erlang (BEAM) runtime.

room_channel.gleam
import beryl/channel
import gleam/json
// One handler per topic pattern, with private typed state.
pub fn room() -> channel.Handler {
channel.handler("room:*", fn(ctx) {
channel.accept(0)
|> channel.on_message(fn(count, msg: channel.Message) {
case msg.event {
"new_msg" ->
channel.next(count + 1, [
channel.broadcast("new_msg", json.int(count + 1)),
])
_ -> channel.stay(count)
}
})
|> channel.with_reply(json.string(ctx.topic))
})
}
  1. Run the live poll

    Download the tutorial app and connect a browser to its first checkpoint. The example includes the server and browser client.

    Run the first checkpoint →
  2. Learn how it works

    Follow one app through the live-poll tutorial. Learn the state-update loop, add voting and server messages, then move to channel handlers.

    Build a live poll →
  3. Build your own app

    Add beryl and the Ewe transport to your project. Use the channel guide for handlers and actions, or adapt a complete example. For control over socket routing, compare the two APIs.

    Installation guide →

Choose handlers or one update function

Register one handler for each topic pattern. beryl/channel routes each event to the correct channel. Each channel keeps private, typed state. You can also route all events in one typed update function.

Choose an API →

Track who is connected

Track connected users across Erlang nodes. A conflict-free replicated data type (CRDT) resolves joins and leaves that happen at the same time.

Presence guide →

Broadcast across Erlang nodes

Send events between connected Erlang nodes through pg process groups. You do not need a separate message broker.

PubSub guide →