strata_client
strata_client is a sans-IO state machine. It encodes outbound Strata frames
and decodes inbound frames. The caller owns the WebSocket transport. The
package targets JavaScript and Erlang.
Correlated mutations
Section titled “Correlated mutations”strata_client/operation adds opt-in request tracking without changing legacy
Command and Event types. Construct a request with
strata_protocol/operation.request(id, mutation), submit it through an
operation session, and feed responses to that session's inbound.
It returns either a normal connection event or an operation outcome.
Keep the session across reconnects. resume marks pending requests unknown
and returns only join frames. After joins, call retry for an unknown delta
or delete. It reuses the exact request ID and payload. Create retries return
UnsafeCreateRetry. timed_out marks a local response deadline without
claiming rejection; a later receipt can resolve it.
The session holds at most 1024 records. forget releases terminal records,
but refuses pending or unknown ones. Do not reuse forgotten IDs. Records are
session-local and do not survive browser termination. The server keeps no
request-ID deduplication cache.
A rejected retry leaves an earlier ambiguous submission unknown. A known terminal result does not regress when a duplicate or late response arrives. See Protocol for retry, acceptance, and persistence semantics.
State machine
Section titled “State machine”Drive the state machine with a Connection value.
- Create state with
strata_client.new(). - Pass the current state and a
Commandtostrata_client.outbound(). - Keep the returned
Connection. - Send the returned JSON text through your WebSocket.
- Pass received text to
strata_client.inbound(). - Keep its returned
Connectionand handle its typedEvent. - Call
strata_client.frames_for_resume()after a reconnect.
Connection functions
Section titled “Connection functions”new() -> Connection: Creates a new, empty connection state.protocol_version() -> Int: Returns0.is_welcomed(Connection) -> Bool: Reports whetherwelcomearrived.session_id(Connection) -> Option(String): Returns the current socket id.server_id(Connection) -> Option(String): Returns the server id.joined_documents(Connection) -> List(String): Returns ids joined through this state value.outbound(Connection, Command) -> #(Connection, String): Creates an outbound frame.CommandincludesJoin,Leave,SendDelta,SendPresence,Create,Delete, andSendPing.inbound(Connection, String) -> Result<#(Connection, Event), StepError>: Processes an inbound frame.EventincludesWelcomed,Snapshot,RemoteDelta,Deleted, presence events,ServerError, andPong.frames_for_resume(Connection) -> #(Connection, List<String>): Generates frames to rejoin documents after a reconnect.
outbound updates local join state before a transport sends the returned
text. If the send fails, the application must retry or correct its connection
state. frames_for_resume keeps joined ids and clears welcome data.
inbound returns ProtocolError(DecodeError) for invalid server text. A
server error frame is a successful decode and becomes
ServerError(code, message).
Typed multi-document apps: strata_client/page_store
Section titled “Typed multi-document apps: strata_client/page_store”strata_client/page_store gives apps that hold several open documents a typed
store, PageStore(page), keyed by document id over the app's own page sum
type. The app supplies merge and to_json; page_store.open/close track
which ids are open, page_store.update applies a local edit, and
page_store.apply_document_event merges an incoming document Event into the
right page and closes it on Deleted. See
Composing Documents.
page_store.update returns None when the id is not open.
apply_document_event returns Result(PageStore(page), ApplyError).
ApplyError has three variants:
NotOpen(document_id)means that the event targets a closed id.MergeFailed(document_id, error)preserves theDocumentError.NotDocumentEventmeans that the event has no document state.
Use apply_event only when you need compatibility with the old no-op behavior.
It calls apply_document_event and intentionally returns the unchanged store
for every error.