How to design a notification system (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
- New notifications while the list is open
- Read and unread across devices
- Pop-up messages from anywhere in the app
- Catching up after being away
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
Notifications for a web app: a list of past items that loads more as you scroll, new ones landing while the list is open, an unread badge, and short pop-ups that any screen or background task can raise. The answer turns on one client store keyed by notification id, fed by both cursor pages and a live stream, with the server as the owner of read state. The pop-ups are a second, smaller system: a single host at the app root fed by a capped queue and announced through a live region.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Does the server push new notifications to the open app, or does the client ask on a timer? | It decides what the live path looks like: a connection to hold and reconnect, or a timer whose interval sets how stale the list and the badge can get. | Assume a live stream per person exists and carries three events: a notification created, a notification read, and the unread count changed. Checks on a timer are the fallback, with at least 60 seconds between checks. |
Where do notification ids come from, and do they sort by time? | The list merges items from pages and from the stream by id, and both the cursor for older pages and the catch-up request lean on an order the client can trust. | The server assigns ids and they sort in creation order, so the client can use an id as a cursor in either direction. Ids from several servers that don't sort cleanly are a follow-up, not the base case. |
How long is the history, and how big is a page? | It sizes the first paint, how often the scroll asks for more, and how many requests a long catch-up takes. | Pages of 50. Assume the history is long enough that the list has to load more as the person scrolls, and that it holds no total. |
Does one person have the app open in several tabs and on several devices at once? | Read state has to agree everywhere; several tabs and devices are what turn it from a single request into a synchronization problem. | Yes: a phone and a laptop, with two tabs open on the laptop. The unread count must agree across them within seconds. |
What does a pop-up carry, and who can raise one? | A plain text toast and one with an action button have different timing and accessibility needs, and the set of callers decides where the host has to live. | A severity level, the message text, an optional action button and a duration, with a close after 5 seconds as the default. Any screen or background task in the app can raise one through a single function. |
What marks a notification read: opening the list, or an explicit action? | A badge that clears when the panel opens is a different state (seen) from one that clears per item (read), and the server has to model whichever we pick. | An explicit action: one item, or all of them up to a point. A separate seen state that clears the badge when the panel opens is a variant worth naming, not building today. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
The unread count agrees across open tabs and devices within seconds | Read state lives on the server, and each client displays the number the server sent, not a sum it keeps locally. A read on one device goes to the server first and then out on the live stream to the others; tabs of the same browser can also pass it to each other directly so none of them waits on the stream. |
No notification shows twice or goes missing after a reconnect | One store keyed by notification id, so an item that arrives on both the stream and a page lands in the same slot; and after any gap, a request for items newer than the latest id the client holds before the stream is trusted again. |
A pop-up never takes focus and is still announced to a screen reader | The host renders into a live region that assistive technology reads without moving focus. Focus stays where the person is working, and an action on a pop-up needs a second home in the UI because the pop-up closes on a timer. |
A tap on a notification answers within 200 milliseconds | Marking read is drawn from client state right away and sent afterward; nothing on the tap path waits for the network. web.dev's good threshold for interaction to next paint is 200 milliseconds or less, so the paint that follows the tap has to show the item as read without waiting on a response. |
A tab left in the background does almost no work | Listen for |
Core entities
| Entity | What it holds |
|---|---|
Notification | One thing that happened to the person, as the server recorded it. |
Page of notifications | One answer to a request for older items. |
Unread count | The badge number, as the server last worked it out. |
Pop-up | A short message raised by some part of the app, queued and drawn by the host. |
Client notification state | Everything the open tab knows about notifications right now. |
The API
| Endpoint | Notes |
|---|---|
| The older-pages call. |
| The catch-up call, used on reconnect, on return from hidden, and as the timer fallback. The server returns what the client missed and the count as of now, so one response fixes both the list and the badge. |
| Marks everything read up to and including an id. Sending the newest id the client has, rather than a list of ids, keeps the request small and leaves anything that landed after it unread. The returned count replaces the local one. |
| One connection per visible tab. In this design we define each event to carry the item or the count it changes, plus an event id the client hands back on reconnect; with server-sent events the browser sends the last event id it saw in a |
| The single entry point into the pop-up system. It appends to a queue the host owns and returns an id, so a caller can close its pop-up later (an upload that fails and then succeeds on retry). Where the host renders is not the caller's concern. |
The high-level design
Step 1 of 4
A list that asks the server for pages
Start with the plainest version: the list component fetches a page of notifications from the server, the badge fetches the count, and scrolling to the bottom asks for the next page. It shows the data flow end to end, and it already carries the fault the rest of the design fixes: with numbered pages, each new item that lands at the top pushes the older ones down a slot, so the next page overlaps the one before it and the list repeats items. Nothing here hears about new items at all.
What breaks first
The deep dives
New notifications while the list is open. Someone is halfway down the list, with older pages still loading, when three new notifications land. What does your list do with them, and what stops any item from appearing twice?
Read and unread across devices. Someone clears all their notifications on their phone while the app sits open in two laptop tabs. What does each screen show, when does it change, and which copy of the count is right?
Pop-up messages from anywhere in the app. A save on the settings page, a failed background upload and a mention from a teammate all want to tell the person something in the same second. How does each message reach the screen, and what does a screen reader user hear?
Catching up after being away. Someone leaves the tab in the background on Friday and comes back on Monday, after dozens of new notifications. How do the list and the badge catch up, and how many requests does it take?
Concepts this guide uses
Sources
- GitHub Docs: REST API endpoints for notifications
- Knock docs: in-app feeds overview
- Knock API reference: overview and pagination
- Knock docs: JavaScript SDK quick start
- notistack: API reference
- notistack: Basic features
- W3C: Understanding SC 4.1.3 Status Messages
- W3C: Understanding SC 2.2.1 Timing Adjustable
- MDN: Broadcast Channel API
- MDN: Page Visibility API
- MDN: ARIA aria-live attribute
- MDN: ARIA live regions
- HTML Standard: Server-sent events
- javascript.info: Server Sent Events
- web.dev: Web Vitals