# Realtime protocol `arbiter.v1` Players and viewers follow a game live, and players play it, over one WebSocket. It is plain WebSocket with JSON text messages, so it works from browsers, React Native, Flutter, native apps and servers. No library is needed. ## 1. Connect 1. **Your server** asks for a token with its API key: - `POST /v1/games/{id}/play-token` with `{ "player": "ext:STU-1042" }` gives a token for one player of that game. - `POST /v1/games/{id}/viewer-token` gives a watch-only token. - Both answer `{ token, expiresAt, color?, url }`. Tokens last 3 hours by default and 12 at most. 2. **The player's app** opens `url`, which is `wss:///rt/v1?token=`. 3. A missing, invalid or expired token is refused with HTTP 401 before the upgrade. Ask your server for a new token. API keys never go into a browser or app. A token is limited to one game, and a play token to one player in it. **Tournament tokens** cover a whole event: - `POST /v1/tournaments/{id}/play-token` with `{ player }` gives one token for a player's whole tournament. The connection moves into each new game as rounds are paired. - `POST /v1/tournaments/{id}/viewer-token` follows every board of the current round, for live-board screens. ## 2. Messages from the server Every message is `{ "type": ..., "data": ..., "ref"?: ... }`. `ref` echoes the `ref` you sent with a command. | type | data | When | |---|---|---| | `hello` | `{ protocol: "arbiter.v1", gameId?, tournamentId?, role: "player" \| "viewer", color?: "white" \| "black" }` | First message after connecting | | `tournament.state` | `{ tournamentId, status, currentRound, roundsPlanned }` | Tournament tokens: second message, then `game.state` for the current game (player) or every current game (viewer) | | `round.paired` | Player: `{ tournamentId, round, gameId, color }`, or `{ tournamentId, round, bye: true }`. Viewer: `{ tournamentId, round, gameIds }` | Tournament tokens. A `game.state` for the new game(s) follows | | `result.recorded`, `result.corrected`, `round.completed`, `standings.updated`, `tournament.completed` | `{ tournamentId, round?, board?, result? }` | Tournament tokens | | `game.state` | The full game, the same shape as `GET /v1/games/{id}`: `fen`, `ply`, `sideToMove`, `whiteMs`, `blackMs`, `clockRunning`, `deadline`, `drawOfferBy`, `result`, `termination`, `moves[]` | Second message, and in answer to `resync` | | `game.move` | `{ gameId, ply, uci, san, fen, whiteMs, blackMs, at, deadline }` | After every move, including your own. `deadline`: when the side now to move loses if it does not move (its first-move window, then its clock) | | `draw.offered` | `{ by: "white" \| "black" }` | | | `draw.declined` | `{ by }` | An explicit decline, or the opponent moving instead of answering | | `game.ended` | `{ gameId, result: "1-0" \| "0-1" \| "1/2-1/2", termination, whiteMs, blackMs, at }` | | | `game.berserk` | `{ gameId, by, whiteMs, blackMs }` | Arena: a side went berserk (half its clock, no increment) | | `arena.you` | `{ paused }` | Arena: you were paused (by request, or for missing a first move) or are back | | `ack` | none | Your command was applied. Its effects arrive as the events above | | `error` | `{ code, message }` | Your command was refused. Nothing changed | | `pong` | none | Answer to `ping` | `termination` is one of: `checkmate`, `resign`, `timeout`, `timeout_insufficient` (the flag fell but the opponent could not mate, so it's a draw), `no_show` (the first move was not made in its window, a forfeit), `stalemate`, `insufficient_material`, `fifty_move`, `threefold`, `agreement`, `adjudication`. ## 3. Commands from the client `{ "type": ..., "uci"?: ..., "ref"?: "" }`. Messages can be up to 4 KB, at up to 10 per second with bursts of 20. | type | Fields | Who | |---|---|---| | `move` | `uci`, e.g. `e2e4`, `e7e8q` | The player to move | | `offer_draw` | none | Either player. One offer per move. Offering back accepts | | `accept_draw` / `decline_draw` | none | The player the offer was made to | | `resign` | none | Either player | | `berserk` | none | Arena, when the event allows it: before your own first move. Halves your clock and drops your increment; a win of 7+ moves earns 1 extra point | | `pause` / `resume` | none | Arena: stop getting new games (a game in progress continues), or come back | | `resync` | `gameId`? (viewers of several games) | Anyone. The server answers with `game.state` | | `ping` | none | Anyone. The server also sends WebSocket pings every 20 s | In a tournament, commands go to the player's current game. Between games (a bye, or the next round not paired yet) they get `no_game`. In an arena, `tournament.state` also carries `format: "arena"`, `endsAt`, `berserk` (allowed or not) and, for a player, `paused`. There are no byes: a player who is not in the latest pairing wave is simply waiting, and the next `round.paired` that includes them moves the connection into their new game. Error codes include `berserk_not_allowed`, `berserk_too_late`, `already_berserk`, `no_game`, `not_your_turn`, `illegal_move`, `game_over`, `draw_offer_limit`, `no_draw_offer`, `forbidden` (viewer), `rate_limited`, `bad_message` and `unknown_type`. ## 4. Clocks - The server owns the clock. Show `whiteMs` and `blackMs` from the latest message. When `clockRunning` is true, count the side to move down locally from the message's `at` time. The server's numbers win at every message. - Each side's first move has a first-move window instead of the clock (default 60 s). `deadline` says when it ends. Missing it loses by `no_show`. - From each side's second move, thinking time is deducted and the increment is added after the move. - A flag fall is decided on the server even if nobody is connected. ## 5. Reconnecting Reconnect with the same token (or a fresh one). The server always starts with `hello` and a full `game.state`, so nothing is lost. If moves arrive while you are rebuilding, drop any `game.move` whose `ply` is less than or equal to the state's `ply`. ## 6. Example ``` → (connect) wss://sandbox.api.arbiter.chessrooq.com/rt/v1?token=eyJ... ← {"type":"hello","data":{"protocol":"arbiter.v1","gameId":"…","role":"player","color":"white"}} ← {"type":"game.state","data":{"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","ply":0,"sideToMove":"white","whiteMs":180000,"blackMs":180000,"clockRunning":false,"deadline":"…","result":"*","moves":[]}} → {"type":"move","uci":"e2e4","ref":"m1"} ← {"type":"game.move","data":{"ply":1,"uci":"e2e4","san":"e4","fen":"…","whiteMs":180000,"blackMs":180000,"at":"…"}} ← {"type":"ack","ref":"m1"} ``` ## 7. Versioning The version is in the path (`/rt/v1`). Within v1, messages and fields are only added. Ignore types and fields you don't know. A breaking change gets `/rt/v2` and 6 months' notice.