Skip to content
Strata is pre-1.0. APIs, protocol, and storage format may change.

strata_server/codec

Strata wire codec for beryl.

Translates strata's flat {"v":0,"kind":"...",...} JSON envelope into beryl's normalised Inbound shape (topic + structural kind + payload) and back out as encoded server frames.

The codec is the only place that knows about the kind ↔ event and doc ↔ topic mappings. Channel handlers in the server see beryl's usual (event, payload, socket) arguments.

Mapping:

Client kindTopicInbound kind
joindoc:Join
leavedoc:Leave
deltadoc:Event("strata.delta")
createdoc:Event("strata.create")
deletedoc:Event("strata.delete")
presence_setdoc:Event("strata.presence_set")
admin_statusadmin:statusJoin
ping"_strata"Heartbeat

For create and delete, <doc> is the parent document id. The channel handler extracts the child id from the payload.

The payload carried in the Inbound is the whole decoded JSON frame as a Dynamic; handlers extract the fields they need (for example state for a delta) using gleam/dynamic/decode.

Server-initiated outbound frames are encoded directly as strata_protocol/server JSON. The codec's encode_push / encode_reply ignore beryl's topic + event arguments because the strata wire shape carries kind (and doc when needed) itself — channel handlers must hand a fully-formed strata server frame as the payload argument. The one exception is the presence_diff push that beryl's runtime emits on its own: its Phoenix-shaped diff is wrapped verbatim in a strata presence_diff envelope, with doc derived from the topic. Replies that beryl's runtime generates itself (for example {"reason": "invalid_topic"} for an unroutable join) are not strata frames; encode_reply wraps those in a strata error frame so clients never see a foreign shape.

Legacy frames have no refs and receive pushes. Opt-in mutations use request_id as beryl's reply ref; handlers resolve it with a correlated operation receipt. Runtime errors retain that correlation too.

Dedicated admin status topic.

pub const admin_status_topic: String

Topic prefix for per-document channels. Concrete topics look like doc:abc-123.

pub const document_topic_prefix: String

Event name for client create frames.

pub const event_create: String

Event name for client delete frames.

pub const event_delete: String

Event name for client delta frames.

pub const event_delta: String

Event name for client presence_set frames.

pub const event_presence_set: String

Special topic used for connection-level frames (currently just ping).

pub const meta_topic: String

Event name beryl's runtime uses for the presence diffs it broadcasts.

pub const runtime_presence_diff_event: String

Build the strata codec value to pass to beryl.config(codec).

pub fn codec() -> codec.Codec

Extract the document id from a doc:<id> topic.

pub fn document_id(String) -> Result(String, Nil)

Build the topic name for a document id.

pub fn document_topic(String) -> String