Skip to content

How to design a live trivia game (frontend)

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Asked at
Practice this question

The problem

A browser client for a live trivia round: up to 100 players join, a question appears on each screen at one moment, a countdown runs for 10 seconds, the server scores the answers and the leaderboard moves. The answer turns on one idea: the server's clock owns the round, and the client's job is to work out how far its own clock is from that clock, draw the reveal and the countdown from the corrected time, and send each answer for the server to judge by when it arrived. Whatever the client times on its own is a convenience for the player, not a verdict.

Clarifying questions

AskWhy it mattersA typical answer

How many people play one round, and how long does each question stay open?

The player count sizes the leaderboard and the lobby list; the question length sets how much a few hundred milliseconds of delay matters, and how long a dropped player can be away and still answer.

Up to 100 players a round, and 10 seconds per question. Both figures come from real products of this kind, so design for a full round of 100 rather than a handful of friends.

How different are the players' connections?

If everyone sat on one office network, showing a question when it arrives would be fine. The spread of delays is what makes the reveal and the timer hard.

Delays from 100 to 500 milliseconds between a player and the server, and a phone can lose signal for a few seconds mid-question. Design for that whole spread; delays in that range are within what internet play sees.

Does the server already run the round, or is the client expected to decide when questions open and close?

It settles where the authority sits. If the client decides timing, nothing stops a player from stretching it; if the server decides, the client is a view onto the server's round and needs a clock it can trust.

The server owns the round: it opens and closes each question on its own clock and judges each answer. You design the client and the contract it needs from that server.

Do points depend on how fast someone answers, or on being right in time?

Speed points make the server's timestamp the whole game and raise the stakes on fairness between fast and slow connections. Right-in-time scoring is more forgiving of a few hundred milliseconds.

An answer scores if it is correct and reached the server in time. Treat speed bonuses as an optional add-on, and say how the timestamp would have to tighten if they were added.

What should happen to a player whose connection drops for a few seconds?

It decides whether the client keeps a way back into the same round, and whether the server must hold a place for an absent player.

They should carry on in the same round with the same score. A reload of the page on the way back is possible, so whatever gets them back in has to survive one.

Will people watch a round without playing, and how many?

Watchers change the fan-out shape: a popular round can draw far more watchers than players, and they don't need everything a player does.

Yes, thousands for a popular round. Treat it as a follow-up once the player flow works, since watchers change what is sent, not how players answer.

When does the client learn the correct answer?

If the correct answer travels with the question, anyone who opens the browser's developer tools can read it before answering, whatever else the design does.

At the reveal, after the question closes, and not before. The question payload holds the text and choices and nothing that gives the answer away.

The requirements menu

RequirementScopeWhy

Join a round and see who else is playing before it starts

Core

The lobby is where the client gets its player id and its way back in, and where the stream connection is first opened.

See each question at the same moment as every other player

Core

The central promise of a live game. If it fails, the fast connection gets a head start on each question.

Answer before the timer ends and learn whether the answer counted

Core

The player needs a verdict, not a guess: the screen has to say whether the tap made it, and the server has to be the one saying so.

Watch the scores and the leaderboard change after each question

Core

The reveal and the leaderboard are the payoff of each question and the moment the client's state changes most.

Watch a round without playing

Optional

Take it if the interviewer raises the audience: it adds a read-only path with its own update rate and a short delay. Leave it until the player flow is complete, since it changes nothing about how players answer.

Points for speed as well as correctness

Optional

Take it if the game wants a race for speed: it makes the server's arrival stamp decide ranking, so the grace for slow connections has to be argued more carefully. Leave it for a right-in-time game.

Show at the reveal how many players picked each choice

Optional

Take it if there's time: the counts arrive in the reveal message and cost one more small view. Leave it if the reveal screen is already busy on a phone.

Keep playing after the page reloads

Optional

Take it: it's a small addition on top of rejoin, a token kept in the tab's storage, and it covers a phone that reloads the page as it comes back from a lost signal.

A host screen shown on a shared display

Optional

Take it for an in-person quiz night, where one big screen shows the question and phones hold the buttons. Leave it for a remote game where each player's screen shows everything.

Matching players into rounds

Out of scope

How players find each other is a separate service. The client receives a round id and joins it.

Writing and checking the questions

Out of scope

Content lives behind the round server. The client renders whatever question it is sent.

Prizes and payments

Out of scope

Money raises fraud and legal questions that would take over the round. Scores are the end of this design.

Chat between players

Out of scope

A second real-time stream with moderation needs of its own. It also gives players a channel for passing answers, which the design would then have to counter.

Non-functional requirements

RequirementWhat it forces

Every player gets the same time to answer, whatever their connection

The client's own measurement of time is not trusted for anything that scores. The server records when each answer reached it and compares that with the closing time it set, allowing a bounded rather than open-ended grace, so a slow connection loses at most a fraction of a second rather than the question.

A tap on an answer shows within 200 milliseconds

The button changes state the moment it is pressed, before the server replies: a selected look and a sending state, with the ack filling in whether it counted. The handler for a tap does no network waiting on the main thread, and the countdown redraws stay cheap so they don't block the tap's paint.

A player can drop for a few seconds and carry on in the same round

A rejoin token saved where a reload keeps it, a reconnecting state the player sees at once, a countdown that keeps running from the corrected clock while the connection is down, and a server call that returns the whole current round state rather than a list of missed messages.

The client cannot learn the answer early or stretch its own time

Nothing in the question payload marks the right choice; the client learns it from the reveal message after the window has closed. The on-screen countdown is cosmetic: it locks the buttons for the player's comfort, while the server's arrival stamp settles whether a tap made it, so editing the page changes what the player sees and nothing more.

Core entities

EntityWhat it holds

Round

One game as the server sees it, mirrored on the client.

Question

What the players see and answer, with its window on the server's clock.

Answer

One player's pick for one question, stamped by the server.

Score

A player's standing after the latest reveal.

Client game state

What one tab knows right now; the single source each screen draws from.

The API

EndpointNotes

POST /rounds/{id}/join → { player_id, rejoin_token, round_state }

Called once from the lobby. The client keeps rejoin_token in the tab's sessionStorage right away, before anything else can go wrong, and renders from round_state so the lobby shows who is already in.

Live round stream: round state, question { id, text, choices, opens_at, closes_at }, reveal { correct, scores }

A WebSocket or server-sent event stream opened after the join. The question arrives a little before opens_at and holds no correct answer; correct appears in the reveal message, with the scores the server computed.

Send answer { question_id, choice } → ack { counted, received_at }

The client sends the pick and the question it was for, and no timing of its own. The server stamps received_at and says in counted whether it fell inside the window plus grace; the screen shows that verdict rather than its own countdown's.

GET /time → { server_time }

A tiny endpoint for measuring the clock offset. The client notes local time before and after the call, takes half the round trip as the one-way delay, and keeps the sample with the shortest round trip. Called a few times at join and again between questions.

GET /rounds/{id}/state?since={version} → the full round state after a rejoin

After a reconnect or a reload the client asks for the current state and replaces its own, rather than replaying what it missed.

The high-level design

streamanswerRound serverClient game stateQuestion screenAnswer buttons

Step 1 of 4

Join, a live stream, and buttons

Start with the smallest client that plays: the lobby calls join, opens the live stream, and writes each message into one client game state that the question screen draws from. The answer buttons send the pick straight to the server. It works on a single fast network, and it has the first failure built in: a question is drawn when its message lands, so a player with a 500 millisecond delay sees it later than a player with a 100 millisecond delay by the difference between the two.

streamtimeanswerackofflinerejoinsnapshotdelayedRound serverClient game stateClock offsetCountdownQuestion screenAnswer buttonsIn-time checkReconnectingbannerTab session tokenLeaderboardWatcher streamWatcher clientsLeaderboard top
The whole design

What breaks first

The deep dives

Everyone sees the same question at the same moment. Two players sit next to each other, one on fast home broadband and one on a weak phone signal, as the next question opens. What sets the moment the question appears on each screen, and how far apart can those two moments be?

A concept that helps here: Clocks and orderingA concept that helps here: WebSockets, server-sent events and pollingA concept that helps here: Where state lives

A timer nobody can cheat. A player sets their device clock back and edits the page so the countdown stays above zero. What in your design decides whether their answer was in time, and how late can an answer reach the server and still count?

A concept that helps here: Optimistic updates and rollbackA concept that helps here: Core Web Vitals

A player who drops and comes back. A player's phone loses signal for four seconds while a question is open, and the page may reload when the signal returns. What do they see from the drop until they are back in the round, and what happens to the answer they tapped while offline?

A concept that helps here: Reconnect and resumeA concept that helps here: Sync queues and client idempotencyA concept that helps here: Heartbeats and presence

Thousands watching one round. A popular round is shared and thousands of people open it just to follow the scores. What strains first, what does a watcher's screen show, and how often does it change?

A concept that helps here: WebSockets, server-sent events and pollingA concept that helps here: Sorted sets and leaderboards

Concepts this guide uses

Sources

  1. Gabriel Gambetta: Client-server game architecture
  2. Colyseus docs: Room
  3. Colyseus docs: Reconnection
  4. Wikipedia: Cristian's algorithm
  5. Wikipedia: HQ (video game)
  6. web.dev: Web Vitals
  7. David Reis: What happens when you make a move in lichess.org
  8. MDN: Window: sessionStorage property
  9. MDN: Window: setTimeout() method
Sign in to report a mistake