Task-oriented snippets for the games modality. Each snippet assumes that you
imported the client:
Join a room as a player
Construct with a playerId to take the player role.
Run the authoritative server
Omit playerId to take the authority role. The authority publishes state and
reads every player’s input.
Broadcast world state on a tick
Open the state publisher once. Then write a self-contained snapshot on each
tick. You drive the clock.
Render incoming state on the client
Subscribe to state. Each callback is one snapshot. Delivery is latest-wins, so
do not block on an older snapshot.
publishInput returns a publisher bound to this player’s id. You do not attach
your own from.
One callback receives all players. The SDK decodes the from header for you.
Send a reliable chat event
Events use a reliable, ordered stream. Use them for data that must not be
dropped.
Receive events with the sender id
subscribeEvents gives you the decoded sender and the payload.
Use multiple event channels
Each channel is an isolated reliable track. Use separate channels for chat and
for gameplay RPCs.
Emit a server-authoritative event
The authority can also publish events. The SDK stamps them with
from = "_authority", so clients know that the server sent them.
Override priority on a write
state and input default to priority 100. Events default to priority 50.
Set a higher priority on a critical event to put it above the rest of its lane.
Track connection state
Pass onState to watch the link. The session reconnects and replays your
channels automatically. You do not re-subscribe by hand.
Stop one channel without leaving the room
Every subscribe* returns a handle. Call close() on the handle to stop one
channel. The other channels stay open.
Leave the room
close() closes the shared session and every channel on it.
Validate a player id before joining
Player ids must encode to 255 UTF-8 bytes or fewer, because the wire header
has one length byte. Check untrusted ids on the client.
To select a channel: if a newer message will replace a dropped message, use
state or input (datagram, latest-wins). If a player will miss a dropped
message, use an event (reliable, ordered).