Skip to Content
Motoko

The socket protocol

The socket carries Motoko's call protocol, the calls and errors of @chia1104/motoko-wire: createSession, listSessions, submit, continue, approve, reply, abort, withdraw, compact, renameSession and deleteSession, with their params, results and error codes, run on the instance by @chia1104/motoko-core's runCall. What the socket adds is its framing, and the two calls and one error that exist because one socket follows several sessions.

Every frame is a JSON text frame whose type starts with motoko:, defined once with zod in @chia1104/motoko-wire/socket, which the server and @chia1104/motoko-client's webSocketTransport both import. The client sends calls; the server answers each, and streams the events of the sessions the connection follows.

FrameDirectionFields
motoko:callclient → serverid, method, params
motoko:resultserver → clientid, result
motoko:errorserver → clientid, error: { code, message }
motoko:eventserver → clientsessionId, cursor, event (an AgentWireEvent)
motoko:endedserver → clientsessionId, error: { code, message }: the follow ended

method is any of wire's calls, or one of the socket's own:

methodparamsresult
subscribesessionId, from? (a cursor)null
unsubscribesessionIdnull

code is any of wire's codes, or the socket's own:

codeWhen
too_many_subscriptionsThe connection follows MAX_SUBSCRIPTIONS sessions already.

invalid_request also answers a frame of Motoko's that does not parse, and internal is emitted as motoko:socket_error. A readonly connection is a readonly caller (see Authorization).

Following

subscribe starts a reader of session.events({ from }) for the connection, replacing an earlier one of the same session. Its result comes before the reader's first event, and the client drops that session's events until it arrives, so a follow started again on the same socket never mixes with the one it replaced. Each event goes out as motoko:event with its cursor. A follow ends with motoko:ended when its session is deleted or cannot be read.

Across reconnects the client's cursor is the one that counts. A new connection follows nothing, so on every open the client subscribes again from the cursor of the last event it folded, and gets what core's stream gives for it: exactly the events after it while the object instance still holds that place in its log (a reconnect during the run), or else the events committed after it and the current in-flight state, which fold into the same view.

Across hibernation the server's copy counts. After every event it sends, the capability writes the session's cursor into the connection's state, which rides the socket's hibernation attachment. When the object wakes (for a message, an RPC call or its alarm) the capability's onStart reads every open connection's state and follows each session on from its cursor, so a socket that stayed connected hears what happens next with no gap and no duplicate, and without asking. An object cannot hibernate while a run works; a reset (a crash, a deploy) closes its sockets, and the client reconnects.

A connection follows at most MAX_SUBSCRIPTIONS (8) sessions, which keeps its state well inside the attachment's 2 KiB. A reader keeps the instance open, so with closeIdleAfterMs it closes once no socket follows a session.

Beside the app's frames

The Lifecycle offers each socket message to every capability before the WebSockets catch-all. MotokoCapability claims a frame only when its type starts with motoko: and it came on an open connection of connections; everything else (the SDK's cf_agent_* and rpc frames, the app's own) goes on to WebSockets and the app's onMessage, which never sees Motoko's. So the app installs the capability and writes no glue, on an Agent and on a plain Durable Object alike. A socket of the experimental capnweb transport carries its frames through Cap'n Web, not as socket messages, and is not served.

In the browser every message listener sees every frame: the app's own (useAgent's onMessage) sees Motoko's too, and skips a type that starts with motoko:.

Motoko keeps what a connection follows under the motoko key of its state, beside the app's keys. An app that sets connection state too updates it with a function that keeps the other keys (connection.setState((state) => ({ ...state, mine }))). State of the app's that is not an object is left alone; its connection's follows then last only as long as the object's instance in memory.