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
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Messages arriving live, in order
- Sending on a bad connection
- A conversation with years of history
- One client, many open tabs
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
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
| Ask | Why it matters | A 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. |
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. |
Non-functional requirements
| Requirement | What 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 ( |
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
| Entity | What 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
| Endpoint | Notes |
|---|---|
| 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. |
| 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 |
| A send. The |
| 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. |
| 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
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.
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?
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 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?
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?
Concepts this guide uses
Sources
- Discord API docs on GitHub: Gateway topic (heartbeat interval, sequence numbers, resume, zombied connections, rate limits)
- discord-api-types: Create Message request body (nonce and enforce_nonce)
- Discord API docs on GitHub: Channel resource (Get Channel Messages limit, Create Message nonce)
- Slack Engineering: Making Slack faster by being lazy
- techinterview.org: Frontend system design, build Discord
- MDN: Broadcast Channel API
- MDN: Web Locks API
- MDN: Overview of scroll anchoring
- MDN: Window: setTimeout() method (timeouts in inactive tabs)
- MDN: ARIA live regions
- web.dev: Interaction to Next Paint (INP)
- Vishwa (dev.to): Frontend system design deep dive, building a web chat application