Command synchronization
Use DocumentSyncClient with an existing command document. The document provides local edits and rollback images; the server extension owns authoritative state.
Client wiring
Given a typed room and document whose absolute writes target command.key:
import { DocumentSyncClient } from "@jolly-pixel/network/client";
const sync = new DocumentSyncClient(room, {
document,
keys: (command) => [command.key]
});
room.join();
await sync.whenReady();Local document changes send automatically. Return null from keys for commands that are not absolute writes. See history's CommandDocument.
Server responsibilities
Implement these in the room's Extension:
- Declare command and sync protocols.
- Send feature snapshots on join and resync. Room
synconly confirms membership. - Attribute commands to their connection, apply accepted edits, and broadcast outcomes, including to the sender.
- Repair refused edits with a snapshot or correction. Include processed sequences in
acksand the refused local sequence inrefused.
The feature supplies versions and recovery state. Client and server share conflict policy, which KeyedCommandArbiter applies on the server. Validate resume data, then return catch-up or a snapshot.
Cleanup
Call sync.destroy() and room.leave(). To keep offline edits across page loads, pass a ChangeOutbox to sync.keepIn() before joining. See CommandSync.