Learn // examples
Examples
Patterns taken from real games built on GameRelay, each with the code that does it and why it's done that way. For a game from scratch, start with the starters; for the full API, see the docs.
From Chess
Two-player chess: quick match or a room code, full rules, draw offers, rematches and chat. About 900 lines in one HTML file. Play chess
- Turn-based multiplayer: chessA two-player chess game with no server code: seats and the board in room state, moves sent to the host and checked against the rules, and a game that carries on when the host leaves.
- Quick match, room codes, draw offers and rematchesGet two players into a game three ways (quick match, a room code, an invite link), then handle draw offers, resignations and rematches that swap colours, all from a real chess game.
From racecar
An arcade street racer for up to 8 players: lobbies, AI rivals, traffic and takedowns. Open source, on the public SDK only. Source on GitHub How its online play works
- A lobby that lives in a roomracecar's online lobby: seats and options in room state, every change checked by one pure function on the host, a server browser with room listings, and a lobby host who isn't always the SDK's host.
- AI players the host drivesBots every player sees the same: the host drives them and sends them as host entities, everyone else draws them like other players, and the next host carries on from where they are.
- One clock for every playerStart a race at the same moment on every screen and keep traffic, weather and hazards in step, using the server's clock instead of each browser's.
- Hits between players: tell the ownerWhen your car hits someone else's, only their screen may move it. How racecar sends bumps and takedowns to each car's owner, avoids pushing twice, and checks what other players send.
Built a game on GameRelay with a pattern worth sharing? Tell us.