> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nodeloc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Real-time matches

> Connect to the game service for matchmaking, rooms, moves, reconnection, and stake settlement

The server decides multiplayer games. The client connects to the game service, sends only actions, and receives views. Validation, dealing, scoring, and settlement all run in the server-side [ruleset](https://docs.nodeloc.com/en/minigames/rulesets.md). The client can show instant hints, but the server's result is final.

Read the game service URL from `nl.system.getInfo().gameService`. In production it is `wss://play.nodeloc.com/v1/ws`. Connecting requires the `game.realtime` scope. `js/net.js` in the Dou Dizhu example is a complete client implementation (reconnection, resending, acknowledgments, heartbeat clock sync) that is not tied to any specific game, so you can reuse it directly.

## Connect

1. Get a game token with `nl.auth.getToken()`.
2. After the connection opens, send `hello {token, build, device?, resume?: {roomId, lastSeq}}` as the first frame. If it is not sent within 10 seconds, the token is invalid, the `game.realtime` scope is missing, or `build` does not match the token, the server sends `kick` and disconnects.
3. Receive `welcome {serverTime, user}`. `user.id` is the anonymous ID and matches the `id` in seats.
4. Send `ping {t}` every 10 seconds; the server replies `pong {t, serverTime}`. The connection drops if no message is received for 45 seconds.

Each person keeps only one connection per game. A new connection kicks the old one (`replaced`).

## Client → server

| type | Fields | Description |
| - | - | - |
| `hello` | `token, build, device?, resume?` | See above |
| `ping` | `t` | Heartbeat |
| `tables` | | Replies with the tables available for this game: `{tables: [{id, ruleset, kind, base, cap, hold, title}]}` |
| `match.join` | `mode?, ruleset?, stake?` | Quick match. `stake` is a table ID; without it the player joins practice tables |
| `match.cancel` | | Cancels matchmaking |
| `room.create` | `ruleset?, config?, private: true` | Opens a friend room; replies `room.created {roomId, roomCode}` |
| `room.join` | `roomCode` | Joins with a 6-digit room code |
| `room.ready` | | Ready. The match starts when all seats are filled and ready |
| `room.leave` | | Leaves before the match starts |
| `room.watch` | `roomId` or `roomCode` | Spectates |
| `room.snapshot` | | Requests a full snapshot (when a sequence gap is detected) |
| `room.auto` | `on` | Turns autoplay on or off |
| `action` | `cseq, action` | An action. The ruleset defines the structure of `action` |
| `chat.quick` | `id` | Quick chat. Only preset IDs are accepted |

## Server → client

| type | Fields | Description |
| - | - | - |
| `welcome` | `serverTime, user` | Connected |
| `match.waiting` | `mode, requeued?, botFillIn?` | Waiting in the queue. `botFillIn` means bots will fill seats after a few more seconds |
| `match.found` | `roomId, seats, bots?, unrated?` | Match found |
| `room.snapshot` | `roomId, seq, seat, seats, started, view?, deadline?, commit, over?` | Full state from this seat's view. `seat` is `-1` when spectating |
| `room.event` | `roomId, seq, events, view, deadline?` | Per-seat filtered events and the new view after each step |
| `room.seats` | `roomId, seats, deadline` | Seat changes (joining, leaving, ready, online, autoplay) |
| `action.ack` | `cseq, ok, error?` | Action acknowledgment, arriving after the `room.event` for the same step |
| `room.over` | `roomId, seq, result, settlement, seed, commit, aborted?` | Match over |
| `chat.quick` | `roomId, seat, id` | Quick chat broadcast |
| `server.draining` | `deadline?` | The service is about to restart; matches in progress can finish |
| `kick` | `reason` | Disconnected: `unauthorized`, `token_expired`, `game_killed`, `build_revoked`, `replaced`, `slow_consumer`, `server_restart`, `moved`, `shutdown` |
| `error` | `code, message?, ref?, url?` | `ref` is the message type that caused the error |

Seat: `{seat, id, name, avatar?, ready, online, auto, bot?}`. A seat with `bot: true` is a server bot, and the UI must label it. `deadline` is the deadline for the current move (Unix milliseconds).

## Sequence numbers and reconnection

* The room sequence number `seq` increases monotonically, and each seat receives `room.event` with consecutive numbers. When you detect a gap, send `room.snapshot`.
* When reconnecting, include `roomId` and `lastSeq` in `hello.resume`. The server resends the missing events, or sends a snapshot if too many are missing.
* With multi-instance deployments you may receive `error {code: "room_moved", url}`. Reconnect to `url`.

## Autoplay and bots

* If the player whose turn it is does not act before `deadline`, the server performs the first legal action for them and marks them as on autoplay (`auto`). Making a move themselves or sending `room.auto {on: false}` cancels it.
* If a quick match still is not full after waiting for a while, the server fills seats with bots and starts (not in friend rooms or ticket tables).

## Fairness

The start event is `{type: "start", data: {commit}}`, where `commit = hex(SHA-256(seed))`. When the match ends, `room.over.seed` reveals the random seed. The client can verify `SHA-256(seed) == commit` and replay the deal from the seed.

## Tables and settlement

The platform configures tables. `kind` is:

| kind | Description |
| - | - |
| `practice` | Practice tables; no game energy involved |
| `stake` | Stake tables: `base × cap` is held on sitting down, settled zero-sum by score × base stake at the end, with a 5% rake on the winner |
| `ticket` | Ticket tables: entry fees go into a prize pool, paid out by placement |

Call `nl.energy.confirmStake` before sitting down at a stake table. `room.over.settlement` gives the actual game energy change for each seat:

```json theme={null}
{ "mode": "stake", "table": "ddz-10", "base": 10,
  "seats": [{ "seat": 0, "delta": -20, "net": -20, "rake": 0 }] }
```

If the hold fails, you receive `error` with `code` set to `insufficient_balance`, `over_limit`, `stake_paused`, or `stake_failed`.

## Common error codes

`unknown_type`, `bad_json`, `not_in_room`, `already_in_room`, `room_not_found`, `room_full`, `room_started`, `unknown_ruleset`, `unknown_table`, `forbidden`, `stake_unavailable`, `insufficient_balance`, `over_limit`, `stake_paused`, `draining`, `game_killed`, `rate_limited`, `room_moved`, `room_gone`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.