Keep every player's view of a multiplayer game the same. Snapshots versus events versus lockstep, delta compression against the last acknowledged snapshot, rebuilding full state on the client, rounding values to cut size, interest management (send only what is near or visible), server clock estimation from ping and pong, exactly-once events over unreliable channels, tick and send rates, and ban...
Download skill (.zip) Files on GitHubNetwork synchronization
Pick a sync style
| Style | How | Good for |
|---|---|---|
| Snapshots | Server sends the state of things 10-30x/s | Action games (default) |
| Events | Send only what happened (“card played”) and apply it | Turn-based, board, card games |
| Lockstep | Everyone sends inputs; every machine simulates the same | RTS with hundreds of units; needs perfectly deterministic code (game-replay-system) |
| Shared state | One saved value everyone reads (Bash shared) |
Party games, boards, lists |
assets/sync.js
import { encodeDelta, createHistory, createReceiver, quantizeEnts, interest, createClock, createEventSender, createEventReceiver } from "./sync.js";
// server, each send tick, per player:
const snap = quantizeEnts(interest(full, player, 900, { always: [player.id] }), { x: 0.1, y: 0.1, a: 0.01 });
history.put(snap); send({ t: "snap", ...encodeDelta(history.get(ackedTick), snap) });
// client:
const full = receiver.receive(msg); // null = missing base → send { t: "ack", tick: -1 } for a full snapshot
send({ t: "ack", tick: full.tick });
- Deltas only carry changed fields, removed entities and new ones, relative to a snapshot the client confirmed. If acks are lost, the server falls back to an older base or a full snapshot; the client never applies a delta to the wrong base.
- Rounding positions to 0.1 and angles to 0.01 cuts JSON size by half or more.
- Interest: only send what is near the player. It also stops wall-hack cheats, since hidden enemies are never sent.
- Clock:
createClock()estimates server time from the fastest recent pings. Interpolation and countdowns useclock.now(), never the phone’s own clock. - Events that must arrive once (kill, pickup, chat over a fast channel):
createEventSenderresends until acked, the receiver drops repeats.
Rates and budgets
| Typical | |
|---|---|
| Server tick | 30-60 Hz (simulation) |
| Snapshot send | 10-20 Hz (100 ms interpolation delay at 20 Hz) |
| Client input send | every tick (60 Hz), small |
| Bandwidth goal | under 10-20 KB/s per player down for mobile data |
Measure: JSON.stringify(msg).length * sendRate. Over budget: round more, drop fields that don’t change (send names once,
not every snapshot), interest radius, lower send rate, then binary encoding as the last step.
Desync checklist
Different code on client and server, floating-point drift in lockstep (use integers or fixed point), events applied twice, state changed in network callbacks instead of the tick, wall-clock time in game logic, and missing “full snapshot” recovery.
Tested
Node tests: delta encoding only changed fields, removed and new entities, exact rebuild, refusing a delta on the wrong base, receiver dropping old packets, quantizing, interest radius with always-sent entities, clock offset within 20 ms under jitter, exactly-once events with acks. Used in the websocket-multiplayer server tests with real sockets.