Bash.tv → GitHub → itch.io

Server-based real-time multiplayer for browser games with Node and WebSockets. A dependency-free WebSocket server, an authoritative game server (rooms with codes, quick play, ready-up, fixed tick, delta snapshots per player, input checks, rate limits, reconnection, spectators, WebRTC signal relay) where you only write the rules, a browser client with reconnect and server clock, and Gem Arena, a...

Download skill (.zip) Files on GitHub

WebSocket multiplayer

In a Bash space with a kit, read playing-together first (the kit has rooms already). Read multiplayer-architecture to confirm a real-time server is the right model.

What’s here (assets/)

server/ws-lite.mjs       WebSocket server for Node, no packages (text/binary, fragments, ping keep-alive, size limit)
server/game-server.mjs   createGameServer({ server, game }): lobbies, sessions, tick, snapshots, checks, spectators
server/lib/              lobby.js, sessions.js, guard.js, sync.js, spectate.js (copies of the other skills' modules)
client/client.js         connect(url, { name, onMessage, onStatus }): hello + token, reconnect, clock, delta rebuild
client/predict.js        prediction and interpolation (from multiplayer-prediction)
example/                 Gem Arena: server.mjs, shared/arena.js (rules), public/ (page, lobby UI, canvas game)

Run the example: cd assets/example && PORT=8080 node server.mjs (use tmux: true), then open_preview on the port. WebSockets work through the Bash preview address.

Writing a game for it

// shared/rules.js: imported by the server AND the page
export function step(p, input, dt) { /* move one player: the SAME code predicts in the browser */ }
export const myGame = {
  inputSchema: { dx: ["num", -1, 1], dy: ["num", -1, 1], fire: "bool?" },   // anything else is refused
  create(room) { return { players: new Map(), time: 90 }; },
  join(st, { id, name }) { st.players.set(id, { x: 100, y: 100, name, score: 0 }); },
  leave(st, id) { st.players.delete(id); },
  move(st, id, input, dt, api) { Object.assign(st.players.get(id), step(st.players.get(id), input, dt)); },  // once per input
  tick(st, dt, api) { st.time -= dt; },                                      // once per tick: world, enemies, timers
  snapshot(st, forId) { return { ents: Object.fromEntries([...st.players].map(([id, p]) => [id, { x: p.x, y: p.y }])), timeLeft: Math.ceil(st.time) }; },
  over(st) { return st.time <= 0 ? { ranking: [...] } : null; },
};

Protocol

Client → server: hello {v, name, token}, create {public, password}, join {code}, quick, list, ready {ready}, start (host), kick {id} (host), leave, input {seq, ...}, ack {tick}, ping {c}, chat {text}, spectate {code}, signal {to, data}. Server → client: welcome {id, token, resumed}, room {room}, rooms, start, snap {tick, base, add, upd, del, ack, time, events}, end {result}, chat, pong, signal, spectating, kicked, error {error, about}.

Tested

node test with real sockets (28 checks): lobby flow, host-only start, prediction equal to the server to 0.1 px over a second of input, deltas, a speed hack refused, 150 queued inputs not moving faster than the speed limit, bad JSON and oversize messages, flood disconnect, signal relay, delayed spectators, drop and resume of the same seat with a full snapshot, old-version refusal, match end with ranking, host handover after the grace period. The page was played in a browser against a bot client.

Run it: node assets/tests/server-test.mjs (set TEST_PORT if 8799 is taken).

Testing your game

Shipping

The page can go on itch.io or any static host; the server needs a host that runs Node all the time (see multiplayer-architecture). Use wss://, set origins to your game’s page origins, keep one protocol version in the client and server, and bump it when messages change.

Pitfalls

Game logic in the message handler (do it in move/tick), sending Maps or class instances (send plain objects), trusting positions from clients, one setInterval per room (one loop for all), no backpressure (the server skips players whose send buffer is over 256 KB), and forgetting that phones drop connections when the screen locks.