Skip to content

How to design a chat app (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 web client for a messaging app: a list of conversations with unread counts, one open conversation, and messages that appear as friends send them. The design turns on a connection that can die without saying so, in front of conversations far longer than any page can draw. The client therefore keeps each message once under its id, numbers the events it receives so it can catch up after a gap, shows a send before the server has answered, and fetches history a page at a time. Everything else, from typing indicators to several open tabs, hangs off those four choices.

Clarifying questions

AskWhy it mattersA typical answer

Is this one-to-one chat, or also group channels with years of history?

The length of the history decides whether paging, virtualization and memory limits are part of the answer. A team channel with years of messages is the hard case for the message list.

Both. A busy channel holds about 200,000 messages, and people do scroll back to find old ones.

Does the server give us a live connection, and what travels over it?

Without one the client has to fall back to polling or another push mechanism, which changes the whole data layer. With one, the question becomes how to notice a drop and catch up.

Yes: a two-way connection that pushes new_message, read and typing events, each with a sequence number, and takes a resume call with the newest number the client saw. History pages and sends go over plain HTTP requests as well.

Will the server accept an id we make on the client for each send, and keep it on the saved message?

It is the difference between a retry that is safe and a retry that posts the message twice. If the server does not do it, the client has to be far more careful about when it retries.

Yes. POST /conversations/{id}/messages takes a client_id, the saved message carries it, and a second send with the same id returns the message already saved.

What kind of connection are people on?

A laptop on office Wi-Fi and a phone on a train fail in different ways. The second is where quiet drops, retries and the outbox earn their place.

Assume both, and assume a drop can happen with no error at all: the socket just goes quiet.

How many conversations can one person be in, and how many tabs might they open?

It sizes what the client holds in memory and whether coordinating tabs is worth the code.

A heavy user is in about 5,000 channels and may have the app open in four tabs at once.

Is reading and writing with no connection in scope today?

Offline support moves the store and the outbox into browser storage and adds a sync step. It is a good follow-up but doubles the design if taken on first.

Not for the first pass. Design for a flaky connection, and be ready to say what changes for a fully offline one.

Who uses it, and what must a screen reader hear?

A live chat that announces each message is unusable with a screen reader; one that announces nothing is unusable too. The answer shapes the live region from the start.

Everyone, including people who rely on a screen reader. New messages must be announced, so you decide how often and in what detail.

The requirements menu

RequirementScopeWhy

See a list of conversations with unread counts

Core

It is the home screen, and the unread counts are the first thing that goes wrong when tabs or devices disagree.

Open a conversation and scroll back through older messages

Core

Opening is the main action, and scrolling back is where a long history meets a finite page.

Send a message and see new messages arrive as they happen

Core

The product. Sending on a bad connection and receiving over a connection that can die are the two biggest discussions.

See who is typing and which messages were read

Core

Both ride the same live connection as messages, and both are cheap once that connection and its event ordering exist.

Keep a draft per conversation

Optional

Take it: it is a small field in the client store and people expect it when they switch conversations. Leave it if the round is running short; it rarely changes the architecture.

Edit and delete a sent message

Optional

Take it if time allows. It is a strong argument for keeping each message once by id, since an edit then has one place to land. Leave it if the live connection is not yet designed.

Read and write with no connection at all

Optional

Take it if the interviewer asks or if the product is mobile-first. It moves the store and outbox into browser storage and adds reconciliation, which can eat the round.

Attachments and image previews

Optional

Take it if the message list is otherwise done: images change row heights after they load, which tests the list design. Leave it in a short round.

Desktop notifications when the tab is in the background

Optional

Take it when tabs are discussed, since a background tab may be throttled and the tab holding the connection decides who notifies. Otherwise leave it.

Search across old messages

Optional

Take it as a follow-up to history: jumping to a result far back is a good test of the list. Leave it otherwise; the search itself belongs to the server.

Voice and video calls

Out of scope

A different stack entirely, with media and signaling of its own.

End-to-end encryption

Out of scope

Key handling and device verification are a design of their own and do not change how the message list works.

How the server stores and fans out messages

Out of scope

A backend question. The client needs the API and the event contract, not the storage behind them.

Non-functional requirements

RequirementWhat it forces

A new message shows without a refresh, in the order it was sent

A live connection that pushes events, and a client that does not trust arrival order: it keeps the server's order, numbers what it receives, and after a gap asks for what it missed instead of showing whatever turns up next. The message list reads from one store rather than appending to itself.

A message the person sent is kept and retried until the server confirms it, and never shows twice

An id made by the client for each send, kept with the message on the server, so a retry carries the same id and the server can recognize it. Unsent messages live in an outbox that outlives a reload, and retries come from there rather than from the message box.

Opening a conversation shows its latest messages without loading its whole history

Paging from the newest message backward with a cursor (before={message_id}), 42 messages a page, and a list that draws the rows near the screen rather than the whole conversation. At that size the 200,000 messages of a busy channel are thousands of pages, so the list also needs a way to let old pages go.

Typing and scrolling answer within 200 milliseconds

Nothing heavy in a key or scroll handler: no walk over a long array, no re-render of the whole list for one new message. For typing, the 200 milliseconds matches the Interaction to Next Paint threshold for a good score, measured from the input to the next frame, so it covers the visible reaction, not the network round trip. INP does not count scrolling, so smooth scrolling is a separate budget the virtualized list has to meet on its own. Typing signals go out after a pause in typing, not on each keystroke.

New messages are announced to screen readers

A polite live region that exists from the first render and gets short, batched updates, such as the sender and a count, rather than the full text of each message in a busy channel. New messages in a conversation that is not open bump a count the region can read instead.

Core entities

EntityWhat it holds

Conversation

One chat the person belongs to, as the sidebar and the header show it.

Message

One message, kept once under its id and referenced from the conversation's ordered list.

Client store

The single source the views read from, in memory, with an optional copy in browser storage.

Outbox

Sends not yet confirmed by the server, kept in browser storage so a reload does not lose them.

The API

EndpointNotes

GET /conversations?cursor → { conversations, next_cursor }

The sidebar. Each conversation comes with its last message, unread count and last read message id, so the list can show state without fetching any history. Paged for people in thousands of channels.

GET /conversations/{id}/messages?before={message_id}&limit=42 → { messages, has_more }

History, cursored by message id rather than by offset so new messages landing at the bottom do not shift the page. In this design the first call omits before to get the latest page. has_more false ends scrolling up.

POST /conversations/{id}/messages { client_id, text } → message

A send. The client_id is made on the client and returned on the saved message; sending the same id again returns that message rather than creating another, which is what makes retries from the outbox safe.

Live connection, server to client: new_message, read, typing, each with a sequence number

The push side. The client records the newest sequence number it has seen and applies each event through the store, not straight to the screen. Typing events are advisory and expire on their own.

Live connection, client to server: resume { last_sequence } → the missed events, then live ones

Sent on each reconnect. The server replays what the client missed in order and then continues live, so the client treats a resumed stream the same as a normal one: each event goes through the same store, where a repeat is harmless.

The high-level design

Conversation listOpen conversationPlain requestsServer

Step 1 of 5

Two views over plain requests

Start with the smallest client that works: a conversation list and an open conversation, each calling the server over plain requests. The list fetches GET /conversations, the open conversation fetches its latest page and posts sends. It shows the shape of the data, and it fails on the first requirement: a message from a friend appears when the page is refreshed and not before.

sendscrolledsendingolderretryeventsresumeeventsConversation listMessages by idMessage listMessage boxOutboxJump to newestPlain requestsConnectionmanagerServerBroadcastchannelOther tabs
The whole design

What breaks first

The deep dives

Messages arriving live, in order. A laptop's connection drops with no error during a busy group chat and comes back a while later. How does your client notice, how does it catch up, and how does each message end up on screen once and in the right order?

A concept that helps here: Reconnect and resumeA concept that helps here: Client caching and normalized storesA concept that helps here: Heartbeats and presence

Sending on a bad connection. Someone on a train sends three messages while the connection keeps dropping and coming back. What do they see on their screen at each moment, and how does the other side end up with those three and no more?

A concept that helps here: Optimistic updates and rollbackA concept that helps here: Sync queues and client idempotencyA concept that helps here: Idempotency keys

A conversation with years of history. Someone opens a team channel with years of messages and scrolls far back to find an old one, while new messages keep landing at the bottom. What does the page do, and what in it gets slow first?

A concept that helps here: Pagination and infinite scrollA concept that helps here: List virtualizationA concept that helps here: Cache eviction

One client, many open tabs. Someone has the app open in four tabs, reads a conversation in one and sends a message from another. What does each tab do, which tab's unread counts are right, and what happens when the tab that owns the connection is closed?

A concept that helps here: Coordinating across tabsA concept that helps here: Leader election and failoverA concept that helps here: Where state lives

Concepts this guide uses

Sources

  1. Discord API docs on GitHub: Gateway topic (heartbeat interval, sequence numbers, resume, zombied connections, rate limits)
  2. discord-api-types: Create Message request body (nonce and enforce_nonce)
  3. Discord API docs on GitHub: Channel resource (Get Channel Messages limit, Create Message nonce)
  4. Slack Engineering: Making Slack faster by being lazy
  5. techinterview.org: Frontend system design, build Discord
  6. MDN: Broadcast Channel API
  7. MDN: Web Locks API
  8. MDN: Overview of scroll anchoring
  9. MDN: Window: setTimeout() method (timeouts in inactive tabs)
  10. MDN: ARIA live regions
  11. web.dev: Interaction to Next Paint (INP)
  12. Vishwa (dev.to): Frontend system design deep dive, building a web chat application
Sign in to report a mistake