# For games

> A short guide to building a multiplayer game on jsflow: what to send fast, what must arrive, who decides, and what to keep on your own server.

jsflow connects up to 50 browsers in a room and moves small messages between them directly. That fits the fast, frequent part of a game: positions, inputs and state a few times a second. It does not decide who is right. This page is the short version; [choosing a state model](https://jsflow.com/docs/concepts/choosing-a-state-model.md) has the full comparison.

## The three rules

1. **Fast traffic on datagrams, must-arrive traffic on `sendReliable`.** Positions and inputs go by `broadcast` or `send`: a lost packet is replaced by the next. A datagram is at most 1,052 bytes, and a broadcast is forwarded by every peer to its own links, so keep the state small (a few hundred bytes at 10 to 20 times a second) or send it less often. A round starting, a command that changes the rules and a result go by `sendReliable`, which is in order for each pair of peers and retransmitted until acknowledged, or reported by `undelivered` after 30 seconds without progress.
2. **One peer decides, and everyone else draws what it says.** The host is **sticky**: a peer that joins adopts the host it hears from, and a peer elects a host only when there is none (an empty room, or the host's `peer` event says `up: false`), for example the lowest id in `room.peers()` with its own. Peer ids are random and reused, so "lowest" does not mean "longest here": never re-elect while a host exists, or a joiner with a lower id would take over. The others send the host their inputs. The host numbers each accepted change and broadcasts the state; peers apply changes in order and ignore a number they have seen.
3. **Anything that must be fair, secret or recorded belongs on your own server.** The host is a browser, and its player can change the rules. Use a peer as host for a friendly game; use your server for scores that count, hidden cards and prizes. jsflow still carries the fast traffic between the players.

## Joining, leaving and reloading

- **A late joiner** gets the whole state from the host, by `sendReliable`, when its `peer` event says it is reachable (in parts, if it is bigger than 64 KiB). Only the host answers. When a peer comes back after a gap, or you get `undelivered`, send the state again.
- **A reload** is a new peer with a new id. Keep the player's identity in your own user id (a [join ticket](https://jsflow.com/docs/guides/join-tickets.md) gives `room.user(peerId)`), not in the peer id.
- **A host that leaves** is noticed through its `peer` event, `up` false: at once if it left or its page went away, after about a second if it crashed or lost its network. If its network comes back within about 20 seconds, the same id returns with `up: true`, so expect `up: false` then `up: true` for the same id. The next peer by your rule becomes host, with a higher **epoch** (a counter and the host's id, compared counter first) that every host message carries, so a message from the old host is ignored, and a host that sees a higher epoch steps down. Every peer resends its current input when it sees a new epoch. The new host carries on from the last state it saw, or starts a **new round** that everyone acknowledges. A host that reloads is a new peer and is not the host.
- **Background tabs.** Phones and Safari freeze hidden pages, and the peer is `away` until it is shown again ([lifecycle](https://jsflow.com/docs/concepts/lifecycle.md)). Decide in your game what an absent player means, and tell your server from your own `visibilitychange` handler if it needs to know.

## Next

[Choosing a state model](https://jsflow.com/docs/concepts/choosing-a-state-model.md) puts the game next to chat and drawing. [Keeping state in sync](https://jsflow.com/docs/guides/keeping-state-in-sync.md) has the code for sequence numbers, snapshots and digests.

Source: https://jsflow.com/docs/guides/for-games/
