Disclosure: I'm the developer of Bear Wars, the game this post is about.
Bear Wars is a turn-based artillery game in the spirit of Worms and Hedgewars that runs in a browser tab. One of the modes is online 1v1: you create a room, send a friend a 4-character code, and you play. Here is how that part is built, without sending the whole game state over the wire.
1. A match is a seed plus inputs
The simulation runs at 60 ticks per second and is fully deterministic. The server picks a random seed when the match starts, and after that the only thing that travels between players is input: move, aim, fire, jump, weapon choice, target point. Two machines that start from the same seed and apply the same inputs tick by tick end up in exactly the same state.
That makes the network traffic tiny, and it gives you replays and tests almost for free.
2. Don't trust Math.sin
Determinism breaks in boring places. JavaScript only guarantees correct rounding for + - * / and Math.sqrt. Math.sin and Math.cos can differ in the last bits between engines, and in a physics simulation those bits grow into a shot that lands somewhere else.
So the core has its own fsin/fcos: range reduction to [-pi/2, pi/2] and a Taylor polynomial up to x^13 (error below 1e-9), built only from operations that are bit-identical everywhere.
3. One player "paces" the turn
It's a turn-based game, so at any moment one player is acting. That player's client is the pacer: it runs the simulation locally with no input lag and sends its frames to the server. The server keeps its own authoritative copy of the match:
- it checks the order of incoming frames;
- it holds back frames that run more than 15 ticks (a quarter second) ahead of its clock;
- it applies them to its copy and forwards them to the other player, who replays them.
If the pacer falls more than 30 ticks behind (lost connection, a hidden tab), the server writes the frames itself ("fill" mode) and tells the pacer to resync from a snapshot. Surrender and forfeit go in the same way, as system actions in the frame stream.
4. Check the hash, fix with a snapshot
Every 60 ticks the state is hashed and compared. If a client disagrees, it asks for a snapshot and continues from there. The same path handles reconnects: a player who drops has 60 seconds to come back, gets a snapshot and catches up.
5. Small frames on the wire
Frames are sent in batches. Most ticks repeat the same held keys, so a batch is run-length encoded: a number means "k more frames with the same held state", an object means one frame that changes something. Each batch starts from a zero held state, so a lost or repeated batch can't corrupt the next one.
6. Room codes people can read out loud
Room codes are 4 characters from a 23-character alphabet with the look-alikes removed: no 0/O, 1/I/L, 2/Z, 5/S, 8/B or U/V. The parser also accepts a whole pasted link with ?room=..., so "send the link" and "say the code" both work.
The server side is a Colyseus room per pair of players, with lobby, rematches and reconnects. The pacing logic itself lives in a plain class with an injectable clock, so it can be tested without a network.
If you want to see it in action, Bear Wars is free and runs at bearwars.org: play the computer, pass one device around, or send a friend a room code. Questions about the netcode are welcome in the comments.


