Handle disconnects and reconnections in multiplayer browser games. Secret session tokens that identify a player across connections, grace periods that keep a seat, resuming a match with a full snapshot, exponential backoff with jitter, phones locking and tabs in the background, keep-alive pings to detect dead connections, a second tab taking over, what other players see, pausing or AI takeover,...
Download skill (.zip) Files on GitHubMultiplayer reconnection
Connections drop all the time: phones lock, wifi switches to mobile data, trains enter tunnels. A good game treats a drop as a pause in that player’s connection, not as leaving.
The player is the token, not the connection
// server (assets/sessions.js)
const sessions = createSessions({ graceMs: 30000 });
on hello: const s = sessions.resume(msg.token) ?? sessions.create({ name }); send({ t: "welcome", id: s.id, token: s.token, resumed })
on close: sessions.drop(s.id); room shows "reconnecting…"
every second: for (const id of sessions.sweep()) removeFromGameForGood(id)
- Keep the token in sessionStorage (one player per tab, survives reload). localStorage would make two tabs one player.
- Resumed players get the room, the match (
startwithresumed: true) and a full snapshot (reset their ack). - Same token on a second connection: the older one is closed with “opened in another tab”.
- Bash kits: the peer id changes on reconnect; keep your own player id as playing-together describes.
Client (assets/sessions.js backoff, websocket-multiplayer client.js)
const wait = backoff({ base: 500, max: 10000 }); // 0.25-0.5 s, 0.5-1 s, 1-2 s ... with randomness
ws.onclose = () => { show("Reconnecting…"); setTimeout(open, wait.next()); }; on welcome: wait.reset()
- Random spread stops 100 phones reconnecting at the same moment after a server restart.
visibilitychange→ visible: reconnect immediately (phones pause background tabs).- Don’t queue inputs while offline: stale inputs replayed later make the player jump. Drop them and re-sync.
- Stop retrying on: old version (show “Reload for the new version”), kicked, other tab.
Detecting dead connections
A phone that loses signal sends no “close”. The server pings every 15 s and drops sockets that didn’t answer the last one (ws-lite does this); the client pings every 2 s for the clock and can treat 3 missed pongs as a drop.
What happens to the game
| Game type | While a player is away |
|---|---|
| Real-time action | Character stands still or AI takes over; invulnerable or removed after the grace period |
| Co-op | Pause for everyone (with a countdown) if the game needs every player |
| Turn-based | Their turn has a deadline; skip or auto-move after it |
| Party rounds | Count them out of “everyone answered” while away; keep their score |
Tell the others: “Ben is reconnecting… 0:24” using sessions.timeLeft(id).
Testing drops
- Node:
client.socket.close()(notclient.close()) simulates a drop; check same id,resumed, full snapshot. - Browser: playwright-cli
network-state-set offlinethenonline. Also lock a real phone for 20 s. - Restart the server mid-match: clients should reconnect and land in the menu with a clear message.
Tested
Node: resume within the grace period, expiry, bad tokens, backoff sequence. With real sockets: a dropped player shows as reconnecting to others, comes back as the same player with a full snapshot, and a player gone past the grace period is removed and the host role moves on.