Skip to content
GitHub1.1k

WebSocket protocol

Games run over Socket.IO on /ws. This page covers the player side, to build your own client, like an ESP32 buzzer. Sessions and joining go through the HTTP API.

loop[each question]POST /api/auth/sessiontokenPOST /api/games/joingameId, ticketconnect (auth: token)player:login (ticket)game:successJoingame:status SELECT_ANSWERplayer:selectedAnswergame:status SHOW_RESULTgame:status FINISHEDPlayerServerPlayerServer

After a disconnection:

POST /api/auth/session (stored token)connect (auth: token)player:reconnect (gameId)player:successReconnectPlayerServerPlayerServer
io("http://<host>:<port>", { path: "/ws", auth: { token } })
  • Get token from POST /api/auth/session, and store it.
  • Refresh it with the same route before every connection. The clientId is kept, so is the player’s seat.
  • The token is only checked on connect: expiring mid-game is fine.
  • Answer Socket.IO pings (every 25 s, 20 s timeout) or you get disconnected.

If the token is refused, you get connect_error with a data.code:

data.code Fix
TOKEN_MISSING Get a token, reconnect.
TOKEN_EXPIRED Refresh it, reconnect.
TOKEN_INVALID Get a new one with an empty body, reconnect.

Socket.IO doesn’t retry after these: call connect() yourself, with a retry limit.

  1. POST /api/games/join with { inviteCode, username } and the token as bearer. Returns { gameId, ticket, username }.
  2. emit player:login { ticket }.
  3. Wait for game:successJoin { gameId, username, gameMode }.
  • ticket: null means you already have a seat: reconnect instead.
  • game:reset <key> means the ticket or game is no longer valid: start over.
  • When the game generates nicknames, username is null in step 1. Read it from step 3.

Sent with game:status { name, data }.

name data Notes
SHOW_START { time, subject } Countdown before the first question.
SHOW_PREPARED { totalAnswers, questionNumber, questionType } totalAnswers is 0 for estimation.
SHOW_QUESTION { question, media?, cooldown } Answers open in cooldown seconds.
SELECT_ANSWER { question, answers, media?, time, totalPlayer, questionType, options?, answersOnly } Answers are open.
SHOW_RESULT { correct, message, points, myPoints, rank, aheadOfMe } Quiz mode only.
WAIT { text } Waiting screen.
FINISHED { subject, top, rank? } End of a quiz.
SUMMARY { subject, totalPlayers, totalQuestions } End of a survey.

In survey mode, SHOW_RESULT is replaced by WAIT { text: "game:answerNoted" }, and the game ends with SUMMARY. Treat SHOW_RESULT and FINISHED as optional and your client works in both modes.

emit player:selectedAnswer { gameId, data: { answerKeys: number[] } }
Question type answerKeys
single The button index, from 0: [1]
multi Every selected button: [0, 2]
estimation The value, clamped to options.min/max: [27]

Only accepted during SELECT_ANSWER, and only the first answer counts.

Refresh the token, connect, then emit player:reconnect { gameId }. Store gameId with the token to survive a reboot.

  • Success: player:successReconnect { gameId, gameMode, status, player, currentQuestion }. status is the current game:status.
  • Failure: game:reset <key>. Join again.

Before the game starts, a disconnected player loses their seat and must join again.

emit player:leave { gameId }

Before the start, the player is removed. After, they are marked disconnected and keep their seat.

Client to server

Event Payload
player:login { ticket }
player:reconnect { gameId }
player:leave { gameId }
player:selectedAnswer { gameId, data: { answerKeys } }

Server to client

Event Payload
game:successJoin { gameId, username, gameMode }
player:successReconnect { gameId, gameMode, status, player, currentQuestion }
game:status { name, data }
game:updateQuestion { current, total }
game:totalPlayers count
game:playerAnswer count of players who answered
game:errorMessage error key
game:reset error key: back to the join screen

Error keys like errors:game.notFound are codes, not messages. Full types: socket.ts, constants.ts.

Only the client that created the game with POST /api/games can send them. Others get game:errorMessage errors:auth.unauthorized.

Event Effect
manager:startGame Starts the game.
manager:advance { gameId } Moves to the next step, or closes open answers early.
manager:kickPlayer Removes a player.
manager:setLock { gameId, locked } Locks or unlocks the room. Confirmed with manager:lockUpdated.

With auto-advance on, the manager receives manager:autoAdvance { seconds, total }, or null when the countdown is cleared.