Skip to main content
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. 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

Server → client

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: Call nl.energy.confirmStake before sitting down at a stake table. room.over.settlement gives the actual game energy change for each seat:
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.