 | berylType-safe real-time channels and presence for Gleam on the BEAM. |
[!IMPORTANT]
beryl is not yet 1.0. Minor releases can change the API or remove features.
Treat beryl as experimental. Try it and send feedback.
System overview
beryl separates a portable channel state machine from its BEAM runtime and
WebSocket transports. Applications use typed socket dispatch or composed
channel handlers. The runtime manages processes, presence, and PubSub;
transport adapters perform WebSocket I/O. See the package contracts
for the interfaces and ownership boundaries.
Install
GitHub hosts the current beryl packages. Hex does not. Add these dependencies
to gleam.toml:
[dependencies]
beryl = { git = "https://github.com/tylerbutler/beryl.git", ref = "main", path = "packages/beryl" }
beryl_ewe = { git = "https://github.com/tylerbutler/beryl.git", ref = "main", path = "packages/beryl_ewe" }
ewe = ">= 9.0.0 and < 10.0.0"
gleam deps download
beryl includes raw dispatch and the recommended beryl/channel composition
layer. The examples serve HTTP and WebSockets with Ewe
and beryl_ewe. If your application uses Mist, use
beryl_mist instead.
The beryl, beryl_mist, and beryl_ewe packages support the
Erlang/BEAM runtime. The separate beryl_protocol package supports Erlang
and JavaScript. It provides pure channel lifecycle transitions without
supervision, PubSub, or presence. See the
Durable Object example for a
JavaScript host that uses Roost for Phoenix framing.
Quick start
import beryl
import beryl/transport/server
import beryl/wire
import beryl/channel
import beryl_ewe
import ewe
import gleam/erlang/process
import gleam/http/response
import gleam/json
import gleam/otp/static_supervisor
type State {
State(room: String)
}
fn room_channel() -> channel.Handler {
channel.handler("room:*", fn(context) {
channel.accept(State(room: context.topic))
|> channel.on_message(fn(state, message) {
case wire.dynamic_to_json(message.payload) {
Ok(payload) ->
channel.next(state, [
channel.discard_reply(message.reply),
channel.broadcast(message.event, payload),
])
Error(Nil) ->
channel.next(state, [
channel.reply_error(message.reply, json.string("Invalid JSON payload")),
])
}
})
})
}
pub fn main() {
let config =
beryl.config(wire.phoenix_codec())
|> beryl.with_frame_rate(per_second: 35, burst: 70)
|> beryl.with_message_rate(per_second: 30, burst: 60)
let assert Ok(#(channels, spec)) =
channel.child_spec(config, handlers: [room_channel()])
let assert Ok(_root) =
static_supervisor.new(static_supervisor.OneForOne)
|> static_supervisor.add(spec)
|> static_supervisor.start()
let assert Ok(_) =
beryl_ewe.handler(channels, server.default_config("/socket/websocket"), fn(_req) {
response.new(404) |> response.set_body(ewe.Empty)
})
|> ewe.new
|> ewe.listening(on: 8000)
|> ewe.start
process.sleep_forever()
}
beryl_ewe.handler serves WebSockets and HTTP on one Ewe listener. It sends
WebSocket requests on /socket/websocket to beryl. Other requests go to the
HTTP fallback, which returns 404 here.
To run a full example with a Phoenix JavaScript client, follow
Run the live poll.
Then use the live-poll tutorial
to learn how it works.
Documentation
Ecosystem
beryl is a server runtime for real-time applications. It manages socket
registration, broadcasts, presence, groups, PubSub, and transport integration.
beryl has a pluggable wire codec. Shared conformance fixtures keep its Phoenix
codec compatible with Roost and Aquamarine.
flowchart TD
fixtures["phoenix_channel_fixtures\ncanonical wire test data"]
roost["roost\nPhoenix channel wire protocol"]
beryl["beryl\nserver runtime"]
aquamarine["aquamarine\nclient runtime"]
gluegun["gluegun\nclient WebSocket transport"]
transport["beryl_ewe or beryl_mist\nserver WebSocket transport"]
fixtures -. "test fixtures" .-> roost
fixtures -. "test fixtures" .-> beryl
fixtures -. "test fixtures" .-> aquamarine
roost --> aquamarine
transport --> beryl
gluegun --> aquamarine
aquamarine <-- "Phoenix channel wire" --> beryl