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. Install beryl

    Install beryl packages from GitHub. Add beryl and one WebSocket transport as Git dependencies. The beryl package includes the channel layer.

    gleam deps download

    Installation guide →
  2. Build your first socket app

    Write a channel handler. Connect the WebSocket transport and a Phoenix JS client. If you want to control routing, compare the two APIs.

    Open the Quick Start →
  3. Run an example

    Use a complete example. Then read the channel guide or the API reference.

    View examples →

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 →