Skip to content

beryl_mist

Mist WebSocket transport for beryl

This module provides the bridge between Mist's native WebSocket handling and the beryl runtime using Mist request and response types directly.

Transport configuration (path, on_connect authentication, origin policy) lives in beryl/transport/server; build a server.TransportConfig with its config builders and pass it to handler or upgrade. This module supplies only the Mist-specific glue: the WebSocket upgrade call, frame sending, and peer IP extraction.

Origin policy migration: beryl/transport/origin.SameOrigin is now SameAuthority. Update explicit constructor references; calls to server.default_config need no change. The default still compares host and explicit port, ignores scheme, and admits requests without Origin. It does not normalize default ports or trust forwarded-host headers. Use server.with_allowed_origins for exact scheme/host/port matching; that policy rejects requests without Origin.

pub fn handler(
beryl.Sockets,
server.TransportConfig(http.Connection),
fn(request.Request(http.Connection)) -> response.Response(mist.ResponseData)
) -> fn(request.Request(http.Connection)) -> response.Response(mist.ResponseData)

Build a combined request handler that serves both WebSocket upgrades and regular HTTP from a single Mist listener.

The returned function inspects and routes each request:

  • It passes WebSocket upgrade requests for the configured socket path to upgrade, which also runs any on_connect callback.
  • It passes all other requests to http_fallback. This includes non-upgrade requests and upgrades for a different path.

This removes the need to write an upgrade guard:

mist_transport.handler(sockets, server.default_config("/socket"), http_handler)
|> mist.new
|> mist.port(8000)
|> mist.start
pub fn upgrade(
request.Request(http.Connection),
beryl.Sockets,
server.TransportConfig(http.Connection),
fn() -> response.Response(mist.ResponseData)
) -> response.Response(mist.ResponseData)

Upgrade a request to WebSocket if it matches the configured path.

Usage in your Mist handler:

fn handle_request(http_request: Request(Connection), sockets: Sockets) -> Response(ResponseData) {
use <- mist_transport.upgrade(http_request, sockets, server.default_config("/socket"))
// Fall through to regular HTTP routing
case request.path_segments(http_request) {
[] -> index_page()
_ -> response.new(404) |> response.set_body(mist.Bytes(bytes_tree.new()))
}
}

Path matching, origin policy, ?vsn version negotiation, connection limits (per-IP and per beryl system on the current node, rejected with 429 Too Many Requests), and the on_connect callback use the shared admission pipeline. See beryl/transport/server.upgrade for the full contract. Enforcement defaults to the real TCP peer IP; forwarded headers are ignored. Use server.with_client_ip_extractor only with an explicit trusted-proxy policy. With either per-IP control enabled, address failures reject with 403 before admission. Without either control, an address is not required.