Channels and history
- A channel is named by a UTF-8 string of 1 to 255 bytes, characters
[A-Za-z0-9:_\-./@]. Its namespace is the part before the first:, or the whole name if it has none (a singleton channel such asannouncements); the namespace maps to a channel declaration in the contract (§12.2), fixing the publication type and presence info type. An empty namespace, or an empty part after a:, is invalid. - Clients subscribe; the server authorizes each subscription.
- Publications are produced by the server (typically from a server method handler or a background job). Clients do not publish to channels directly in v1 [open].
- Channels are distinct from groups: groups are server-side targeting sets with no client visibility, history or presence.
SUBSCRIBE (0x30) and SUBSCRIBED (0x31)
Section titled “SUBSCRIBE (0x30) and SUBSCRIBED (0x31)”SUBSCRIBE header: rid (MUST), channel (MUST), since (MAY, requires history), presence (MAY, requires presence), meta (MAY). Data section: optional subscription parameters, one value of the channel declaration’s subscribe type (§12.2), empty if the declaration has none.
Server processing:
- Validate name and namespace → REQUEST_ERROR INVALID_ARGUMENT.
- Decode the subscription parameters → REQUEST_ERROR INVALID_ARGUMENT.
- Already subscribed on this session → REQUEST_ERROR ALREADY_EXISTS.
- Subscription limit (
max_subs) → REQUEST_ERROR RESOURCE_EXHAUSTED. - Authorization → REQUEST_ERROR PERMISSION_DENIED (or UNAUTHENTICATED for an anonymous session).
presence: truethat cannot be served → REQUEST_ERROR RESOURCE_EXHAUSTED or FAILED_PRECONDITION (§10.6).- Encode the snapshot and check the presence info (§12.3), then register the subscription and reply SUBSCRIBED. A snapshot that does not encode, or presence info that the entry rule refuses, is answered REQUEST_ERROR INTERNAL: the subscription is not registered and no join is emitted (§12.3).
SUBSCRIBED header: rid, channel, seq (if resuming enabled), epoch (text), offset, and recovered (only if since was given). offset is the offset the client’s state corresponds to after SUBSCRIBED: the snapshot’s offset when a snapshot is included, otherwise the channel’s current top offset.
SUBSCRIBED data section: if the channel namespace declares a snapshot type (§12.2), the state of the channel exactly as of offset, encoded with the session codec; otherwise empty. The snapshot’s offset MAY be older than the current top (for example when the snapshot is read from an application database that lags slightly); the server then sends the publications above it from history. Snapshot-on-subscribe removes the “fetch state, then subscribe” race: the client applies the snapshot and then every publication with offset > SUBSCRIBED offset. When since was given and recovery succeeds, the server MAY omit the snapshot (the replay suffices); when recovery fails (recovered: false) a server that supports snapshots MUST include one, which makes the failure self-healing. When a snapshot is included, no since replay is sent; the snapshot supersedes it. If the snapshot’s offset is older than the retained history, the server MAY retry the snapshot provider; otherwise it answers REQUEST_ERROR UNAVAILABLE instead of SUBSCRIBED.
Frames that follow, in this order:
- If presence was requested: a PRESENCE snapshot (§10.6).
- If
sincewas given, recovery succeeded and no snapshot was included: replayed PUBLICATION frames with offsets fromsince.offset + 1up to SUBSCRIBEDoffset, in order. - Every PUBLICATION with an offset greater than SUBSCRIBED
offset, from history or live, in order and without gaps.
The server MUST ensure no publication is lost or duplicated between the replay and live phases (e.g. by buffering live publications for this subscriber until the replay has been written).
UNSUBSCRIBE (0x32) and UNSUBSCRIBED (0x33)
Section titled “UNSUBSCRIBE (0x32) and UNSUBSCRIBED (0x33)”- Client:
UNSUBSCRIBE {1: rid, 9: channel}. Server repliesUNSUBSCRIBED {1: rid, 9: channel}. Unsubscribing a channel that isn’t subscribed is not an error. - Server-initiated:
UNSUBSCRIBED {9: channel, 6: code, 7: msg}withoutrid. Codes (unsubscribe reasons, separate registry, §15.3):0normal,1permission revoked,2resubscribe required,3channel closed. - On code 2 the client SHOULD resubscribe immediately, using
sinceif it has a position. - After UNSUBSCRIBED the server MUST NOT send further PUBLICATION or PRESENCE frames for that channel on this session.
PUBLICATION (0x34)
Section titled “PUBLICATION (0x34)”- Header:
channel,offset, andseqwhen resumption is enabled. - Data section: the publication value, typed by the channel declaration.
- Offsets are unsigned integers, strictly increasing by exactly 1 per publication within an epoch. The first publication in an epoch has offset 1; a channel with no publications in its current epoch reports top offset 0.
- An undecodable publication value is handled per §8.10.
- A publication value that a codec the endpoint offers (§13.8) cannot encode is refused to the publishing code when it enters the server, and the offset does not advance (§12.3).
History and recovery (capability history)
Section titled “History and recovery (capability history)”- Every channel has an epoch and an offset counter whether or not it retains history;
historycontrols only how far backsincecan reach. A channel without history answers everysincewithrecovered: false. - A channel with history keeps its most recent publications, bounded by count and/or age (server configuration).
- An epoch identifies a continuous history. It changes whenever history continuity is broken (history store lost, channel reset). Clients compare epochs as opaque strings.
- Recovery: a client that saw publications up to
(epoch E, offset N)subscribes withsince {10: E, 11: N}.- If the server still has epoch E and publications N+1 onward:
recovered: trueand those publications are replayed. - A
sincewhose offset is above the channel’s current top offset in the same epoch is answeredrecovered: false. - Otherwise:
recovered: false, no replay. The client MUST treat its local state for that channel as stale and re-fetch it by other means (typically a server method returning a snapshot plus the offset it corresponds to).
- If the server still has epoch E and publications N+1 onward:
- Clients SHOULD track
(epoch, offset)per subscribed channel from every SUBSCRIBED and PUBLICATION they process. - Recovery works across session loss, node failure and server restarts, provided the history store survives. This is its purpose; session resumption (§7.8) covers only short transport interruptions.
- If a server loses continuity for a live channel, it MUST send UNSUBSCRIBED code 2 (resubscribe) to its subscribers.
- Sequencing constraint: because offsets increase by exactly 1, every channel, with or without history, needs a single sequencer (one owner assigning offsets) at any moment, even in multi-node deployments. Implementations choose the mechanism (channel ownership, a store that assigns offsets atomically such as an append-only log); the protocol only requires the result. An offset gap observed by a client is a server bug, not a recoverable condition.