Skip to main content
Worker-safe. Defines every message that flows between a hosted client and the server. Shared by @openturn/client, @openturn/server, and the generated Cloudflare Worker. You import this package when you write custom transport (your own WebSocket server, a relay, a test harness). Typical app code does not.

Install

Client-to-server messages

ClientAction<TEvent, TPayload>

A player action dispatch.

SyncRequest

Request a full snapshot of the match (used on initial connect).

ResyncRequest

Request a resync since a specific revision (used after reconnect).

SaveRequest

Ask the server to produce a save envelope for the current match.

ProtocolClientMessage

Discriminated union of ClientAction | SyncRequest | ResyncRequest | SaveRequest.

Server-to-client messages

MatchSnapshot<TPublicState, TResult>

Full match state — the observer shape. No type discriminator; snapshots are routed as server messages directly.

PlayerViewSnapshot<TPublicState, TResult>

Extends MatchSnapshot with playerID. G is the player-scoped view (hidden fields stripped).

BatchApplied<TPublicState, TResult>

One batch of steps committed by the runtime.

ActionRejected

SaveReady / SaveError

Responses to SaveRequest.

ProtocolServerMessage<TPublicState, TResult>

Union: MatchSnapshot | PlayerViewSnapshot | BatchApplied | ActionRejected | SaveReady | SaveError.

ProtocolErrorCode

Replay-adjacent types

  • ProtocolStep, ProtocolEventRecord, ProtocolActionRecord, ProtocolInternalEventRecord, ProtocolQueuedEventRecord
  • ProtocolObservedTransition, ProtocolTransitionCandidateEvaluation, ProtocolTransitionFamilyEvaluation
  • ProtocolGraph, ProtocolGraphNode, ProtocolGraphEdge
  • ProtocolDerivedState, ProtocolControlMeta, ProtocolRuntimeState, ProtocolHistoryBranch
All are JSON-compatible and have matching *Schema exports for runtime validation.

Zod schemas

Every type has a *Schema counterpart, used both internally and by consumers that need runtime validation of untrusted input. Messages: ClientActionSchema, SyncRequestSchema, ResyncRequestSchema, SaveRequestSchema, ProtocolClientMessageSchema, BatchAppliedSchema, ActionRejectedSchema, SaveReadySchema, SaveErrorSchema, ProtocolServerMessageSchema. Snapshots: MatchSnapshotSchema, PlayerViewSnapshotSchema. Runtime bodies: ProtocolStepSchema, ProtocolObservedTransitionSchema, ProtocolEventRecordSchema, ProtocolActionRecordSchema, ProtocolInternalEventRecordSchema, ProtocolQueuedEventRecordSchema, ProtocolTransitionCandidateEvaluationSchema, ProtocolTransitionFamilyEvaluationSchema. Derived / control: ProtocolControlMetaSchema, ProtocolControlMetadataEntrySchema, ProtocolDerivedStateSchema, ProtocolRuntimeStateSchema, ProtocolHistoryBranchSchema.

Parse and stringify helpers

Converting from core

Helpers for translating core runtime values into protocol shapes:
  • protocolizeGameSnapshot(snapshot, options)
  • protocolizeGameStep(step)
  • protocolizeGameActionRecord(record)
  • protocolizeGameEventRecord(event)
  • protocolizeGameObservedTransition(transition)
  • protocolizeGameGraph(graph)
  • protocolizeValue(value)
These are the functions @openturn/server uses when producing messages.

Core scalars and identifiers

  • MatchID — stable per-room identifier (string).
  • Revision — monotonic counter the server advances per committed batch (number).
  • ProtocolPlayerID — protocol-scoped PlayerID (string).
  • ProtocolValueJsonValue alias for every payload that crosses the wire.
  • ProtocolErrorDetail — structured detail emitted alongside an ActionRejected.error.

Lobby protocol

Re-exports from ./lobby: Types: LobbyPhase, LobbySeat, LobbyCloseReason, LobbyRejectionReason, LobbyClientMessage, LobbyServerMessage, LobbyClose, LobbyClosedMessage, LobbyLeaveSeat, LobbyRejectedMessage, LobbySetReady, LobbyStart, LobbySetTargetCapacity, LobbyStateMessage, LobbyTakeSeat, LobbyTransitionToGameMessage. LobbyStateMessage carries { minPlayers, maxPlayers, targetCapacity } describing the room’s player range. LobbySetTargetCapacity is host-only and mutates targetCapacity within [minPlayers, maxPlayers]. New rejection reasons: target_below_min, target_above_max, bad_target. Schemas: LobbyClientMessageSchema, LobbyServerMessageSchema, LobbyClosedMessageSchema, LobbyPhaseSchema, LobbyRejectedMessageSchema, LobbySeatSchema, LobbyStateMessageSchema, LobbyTransitionToGameMessageSchema. Helpers: parseLobbyClientMessage, parseLobbyClientMessageText, parseLobbyServerMessage, parseLobbyServerMessageText, stringifyLobbyClientMessage, stringifyLobbyServerMessage, isLobbyClientMessageText. Used by LobbyRuntime on the server and useRoom() on the client.

See also