Keeps multiplayer game worlds consistent between server and clients. Covers sync strategies (dumb client, lockstep, snapshot interpolation, client prediction with server reconciliation, lag compensation) with TypeScript boilerplate for delta encoding, rollback history buffer, and fixed-timestep client loop.
Ships in game-dev. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
The server owns the game state. Clients send inputs, never results. Every technique below hides latency on top of that rule; none of them replaces it.
Prerequisites: a server tick loop and authority model (game-backend-architecture). For fanning deltas out across several server processes, see the fan-out section of game-backend-architecture.
Decide this first. Most games combine rows: prediction for the local player, interpolation for everyone else, lag compensation for hits.
| Game situation | Strategy | Cost and limits | Boilerplate |
|---|---|---|---|
| Turn-based or slow (strategy, cards, board games) | Dumb client: send input, wait for the server's new state, render it | Input lag equals round-trip time. Fine when turns are slow | Not needed; skip prediction |
| Your own avatar in a real-time game | Client prediction plus server reconciliation: apply input locally, tag it with a sequence number, replay unacknowledged inputs on each server state | Mispredictions cause corrections; smooth small ones, snap large ones | StateSyncEngine.reconcile, ClientGameLoop.handleServerUpdate |
| Other players whose speed and direction change instantly (shooters) | Entity interpolation: render them in the past, between two received snapshots | They appear about one interpolation period behind. Source defaults to 100 ms (cl_interp 0.1) | RollbackBuffer.getBracket over received snapshots |
| Entities with predictable motion (vehicles, ships) | Dead reckoning: extrapolate from last speed and heading, correct on the next update | Wrong when motion changes. Cap how long you extrapolate | Pattern only, see references/client-architecture.md |
| Hit detection against entities shown in the past | Lag compensation: the server rewinds the world to the shooter's view time and tests the hit there | A target can be hit just after reaching cover | Not implemented; use RollbackBuffer for the history |
| 2-4 players, simulation you can make deterministic | Deterministic lockstep: send inputs only | Bandwidth independent of object count. Everyone waits for the slowest player's input; float differences break it | Not implemented |
| Physics-heavy co-op with no competitive stakes | Distributed authority with state synchronization: each player owns the objects it touches | Less secure; only for non-competitive play | Not implemented |
The preset values in templates/sync-strategies.ts (tick rates, byte budgets, buffer sizes) are starting points to measure against, not benchmarks.
Keep entities in Record<id, entity>, not arrays. An insert into an array shifts every later index and the whole tail diffs as changed.
interface MyGameState extends Record<string, unknown> {
tick: number;
players: Record<string, { x: number; y: number; vx: number; vy: number }>;
}
import { StateSyncEngine, type PlayerInput } from './state-sync';
const engine = new StateSyncEngine<MyGameState>(initialState, {
tickRate: 20,
snapshotInterval: 1,
historyLength: 64,
interpolationDelay: 2,
});
// Each server tick. With snapshotInterval: 1, tick() already stores this
// tick in the history; snapshot() replaces that entry (the buffer keeps one
// entry per tick) and returns { tick, state, timestamp } to send.
engine.tick((state, tick) => simulate(applyInputs(state, pendingInputs), tick));
const snap = engine.snapshot();
Send each client the new state as a delta (step 3), with the sequence number of the last input processed for that client.
Over an unreliable transport such as UDP, encode each client's delta against the last snapshot that client acknowledged, not against the previous tick. A client that lost the previous packet cannot apply a delta built on it. Keep a per-client acknowledged tick and a history of sent snapshots (RollbackBuffer). Send a full snapshot on join and after sustained packet loss.
Over WebSocket, deltas against the previous tick are safe: WebSocket runs on TCP, which delivers every message in order, so the client always holds the previous state. This is what game-backend-architecture does. Resend a full snapshot when a client reconnects.
import { encodeDelta, decodeDelta } from './delta-encoder';
const delta = encodeDelta(ackedBaseline, currentState); // server, per client
const next = decodeDelta(ackedBaseline, delta); // client, same baseline
toBinaryDelta only frames the JSON delta in a byte buffer. It does not compress. Quantize floats yourself if bandwidth matters.
clientTick is your own counter, advanced once per fixed step (step 7). applyLocalInput does not advance the engine's tick.
let sequence = 0;
const input: PlayerInput = { tick: clientTick, playerId, actions, sequence: ++sequence };
engine.applyLocalInput(input, applyInput); // predict now
send(input);
// On each authoritative state from the server
const result = engine.reconcile(serverState, serverTick, lastProcessedSequence, applyInput);
if (result.corrected) startCorrectionSmoothing();
reconcile discards inputs up to lastProcessedSequence and replays the rest on top of the server state. Keep irreversible outcomes (death, item pickup) until the server confirms them.
Store the snapshots you receive, keyed by server tick, and render remote entities a fixed delay behind the newest one. Make the delay span at least two snapshot intervals, so one lost snapshot still leaves two to interpolate between. A longer delay survives more consecutive losses at the cost of latency.
import { RollbackBuffer } from './rollback-buffer';
const received = new RollbackBuffer<MyGameState>(32);
received.push(serverTick, serverState); // on each server snapshot
// Each render frame. estimatedServerTick is fractional, from your clock sync.
const renderTick = estimatedServerTick - delayTicks;
const { before, after } = received.getBracket(renderTick);
if (before && after) {
const alpha = (renderTick - before.tick) / (after.tick - before.tick);
render(lerpEntities(before.state, after.state, alpha));
}
Do not use engine.getInterpolationStates for this on the client: it reads the engine's own history, which holds the local player's predicted states.
When both ends are missing for longer than your extrapolation cap, freeze the entity and mark it stale. Source extrapolates for at most 0.25 s of packet loss (cl_extrapolate_amount).
Snap the simulation state to the server's. Smooth only the rendered position: reduce small errors gradually and correct large ones fast, since a slow correction of a large jump disorients players.
Run the simulation at a fixed tick with an accumulator; render at display rate and interpolate with the leftover fraction. boilerplate/game-loop-client.ts does this. With a rendering engine that runs its own frame loop (Phaser, PixiJS, three.js), never call start(): call step(now) from the engine's per-frame callback and draw from getRenderState(), so one loop drives both. game-client covers the per-engine wiring. The full client walkthrough (input sampling, prediction, dead reckoning, the loop) is in references/client-architecture.md.
In a world with many entities, broadcasting every change to every client does not scale. Filter per client, then fit what remains into a byte budget.
Area of interest, first step: a uniform grid.
Byte budget, second step: a priority accumulator, for when the visible set still does not fit.
bandwidth in templates/sync-strategies.ts, then measure.., so a state key that contains a dot ("x.y") decodes as a nested x.y path and the old key stays. Keep dots out of state keys.StateSyncEngine stores state with structuredClone. Keep state to plain data: no class instances, functions or DOM nodes.delta-encoder.ts refuses any path or key named __proto__, constructor or prototype and throws instead of applying the delta; keep that check if you change the path format.prev.players === current.players, skip the subtree.boilerplate/state-sync.ts: copy when you need the server or client engine. StateSyncEngine with tick, snapshot, delta, local prediction, reconciliation, rollback-and-replay, and interpolation over its own history.boilerplate/delta-encoder.ts: copy when you send state diffs. encodeDelta, decodeDelta, toBinaryDelta, fromBinaryDelta, deltaByteSize, deepEqual.boilerplate/rollback-buffer.ts: copy when you need tick-indexed history for rollback, acknowledged baselines or lag compensation. Ring buffer with push, getAt, rollbackTo, getBracket, getRange.boilerplate/game-loop-client.ts: copy when you build the browser client. ClientGameLoop with a fixed timestep, input ring buffer, prediction and handleServerUpdate reconciliation. It reports staleness (isExtrapolating) but does no dead reckoning itself. Runs its own requestAnimationFrame loop (start()) or is stepped from an engine's frame callback (step(), getRenderState(), resetClock()).templates/sync-strategies.ts: copy when you configure interpolation, prediction, rollback and bandwidth settings. Has action, turn-based and MMO presets. The presets do not configure area of interest; step 8 does.references/client-architecture.md: read when you build the client side: input sampling, the prediction loop, dead reckoning and fixed-timestep rendering, with a sequence diagram.references/sources.md: read when you need the talks and articles behind these rules.game-backend-architecture: the authoritative server loop and rooms these clients sync with.game-client: the browser client that renders this state and feeds it input, on plain Canvas/WebGL, Phaser, PixiJS or Three.js.boilerplate/delta-encoder.ts (opens in a new tab)boilerplate/game-loop-client.ts (opens in a new tab)boilerplate/rollback-buffer.ts (opens in a new tab)boilerplate/state-sync.ts (opens in a new tab)references/client-architecture.md (opens in a new tab)references/sources.md (opens in a new tab)templates/sync-strategies.ts (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
/game-dev:game-state-syncfrom game-dev
npx claudepluginhub fcsouza/agent-skills --plugin game-dev39 skills in this plugin
/betterauth-integrationCovers the game-specific layer of Better Auth: player, moderator and admin roles, session checks on game routes and WebSocket handshakes, and keeping the auth session apart from the game session. Use when a game needs player login, moderator or admin powers, or authenticated real-time connections, and the project uses Better Auth./bullmq-game-queuesBackground job design for game backends on BullMQ and Redis: one queue per game domain, idempotent reward fulfilment, and cron schedulers for daily rewards and season ends. Use when a game needs matchmaking requests, reward grants, match results, notifications or analytics processed off the request path, or recurring and timed jobs such as daily login rewards, leaderboard snapshots and season rollovers./Other plugins' skills for the same job