Let people watch multiplayer matches. Spectators joining by room code without taking a seat, a delay buffer so watchers can't feed live positions to players, a spectator view that hides secret information, a camera that follows a player, cycles between players, pans freely or fits everyone, spectator UI (names, scores, who is followed), and streaming-friendly views. Tested module, built into th...
Download skill (.zip) Files on GitHubSpectator mode
Server side (assets/spectate.js)
import { createDelayBuffer, spectatorView } from "./spectate.js";
const delayed = createDelayBuffer(3000); // 3 s behind: enough to stop "he's behind the wall!" calls
each send tick:
delayed.push(Date.now(), spectatorView(snapshot, { hiddenFields: ["hand", "role"] }));
const late = delayed.take(Date.now()); if (late) for (const s of spectators) s.send(late);
- Spectators are not players: no seat, no inputs, don’t count toward full rooms or “everyone answered”.
- Send them full snapshots (no per-viewer deltas): simple, and there are few of them. Cap spectators per room (e.g. 20).
- websocket-multiplayer’s game server does this already:
{ t: "spectate", code },spectatorDelayMsoption, andsnapshot(state, null)is the spectator view. - Delay: 0 for friends’ games and casual play, 2-5 s for competitive games, 30-120 s for tournaments.
Camera (client)
import { createSpectatorCamera } from "./spectate.js";
const cam = createSpectatorCamera({ width: 960, height: 540 });
each frame: cam.update(dt, ents); ctx.save(); cam.apply(ctx); drawWorld(); ctx.restore();
Tab / tap a name: cam.next(); Shift+Tab: cam.prev(); drag: cam.pan(dx, dy); O: cam.mode = "overview"
Modes: follow (a player; picks the first when none is chosen), free (drag to pan), overview (fits all players with a zoom limit).
UI
- A clear “Watching” label with the room code, so nobody thinks they’re playing.
- Player list with scores; tap a name to follow them. Show whose view it is.
- Phones: tap left or right of the screen for previous or next player.
- “Join next match” button when the room is back in the lobby.
- Stream mode: bigger names, no own UI, overview camera.
Tested
Node tests: delay buffer returns the newest item at least the delay old and drops older ones; hidden fields removed; follow, cycle and overview camera. With real sockets: the spectator receives snapshots about 500 ms late (test setting).
Pitfalls
Sending spectators the players’ private data, spectators counted as players, no delay in competitive games, and a camera that jumps (it eases here).