Bash.tv → GitHub → itch.io

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 GitHub

Multiplayer 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)

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()

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

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.