Chapter 27
The web client
Ana has three chat tabs open in her browser, and during a meeting one of them sits in the background. Are three tabs three devices, or one?
This chapter is a first draft. The author is still checking it, and its text and numbers may change.
At work, Ana chats in her browser. She has one tab open on the hiking group and another for her chat with Ben; then she clicks a link and a third one appears. In the afternoon she is in a meeting for an hour, and the hiking group tab stays open the whole time, hidden behind other pages.
After the meeting she picks up her phone: the hiking group shows nothing unread. Yet more than sixty messages came in during that hour, and she has not seen one of them.
In a browser, a tab is not a device. The client in the twenty-odd chapters so far was written for a phone: one app, one connection, one screen. Copied as it is onto the web, each tab runs its own copy, and three Anas live in the browser at once. This chapter merges them into one.
1. Different web clients need different things
| Web client | Local history | Connection | How to wake it once closed |
|---|---|---|---|
| A full web app (the web client this series builds) | A database in the browser, used as a cache | One per browser, shared by its tabs | Web push, where the browser allows it |
| A light client (a support widget on a website, chapter 43) | None; the latest page is fetched on open | One per page | It cannot be woken; whether to send an email or a text is up to the business |
| Mini programs (apps that run inside a host app such as WeChat), web pages inside an app | Whatever the host allows | The host’s APIs | The host’s channel |
This chapter is about the first row. The support widget has one more twist: it is embedded in someone else’s website. Since version 115, Chrome partitions storage by top-level site: the same widget embedded on two websites gets two separate stores that cannot see each other (explainer). So its login token has to be issued by the host website’s server (chapter 25); it cannot count on the browser remembering it.
2. Watch it break: a separate client in each tab
One connection and one copy of the state per tab is what most websites do by default. The designs of earlier chapters actually catch quite a lot here:
- If two tabs resend the same message from the outbox, the message ID is the same and the server’s dedupdedup去重When a message ID arrives again, return the first result instead of storing another copy. The network can only deliver at least once; dedup makes each message show once.See the glossary (chapter 4) keeps it from appearing twice.
- If two tabs write the same page of messages into the local database at once: (conversation, seq) is a unique key there, so the second write of a message lands on the row already there; and the cursor that records how far the history is filled only moves up (chapter 7), so whichever tab writes last cannot move it back. Nothing gets mixed up either.
What they do not catch:
- A hidden tab reads for her. Chapter 14’s rule is “report what has been shown”, and it assumes the screen is in front of her. The hiking group gets about 1,000 messages a day; over 16 waking hours that is about 62 an hour. That tab has the conversation open, so it reports a read for every message that arrives; the unread count is the latest seq minus the read position (chapter 14), the read position keeps up with the latest seq, and her phone shows 0 unread the whole time. This one has nothing to do with the number of tabs; a single tab would do the same. It is a phone rule carried over to the web.
- One message, three notifications. Every tab gets the push, and each pops up its own desktop notification.
- Three “devices”. Each tab has its own connection, so the server sees three devices (chapter 16): every message is pushed three times, there are three sync cursors, and when the computer wakes up it syncs three times.
- The heartbeat is slowed. Once a page has been hidden for more than 5 minutes, Chrome lets chained timers (timers set from inside other timers, as a repeating heartbeat is) fire only once a minute (Chrome 88). Chapter 23’s heartbeat every 4 minutes can slip to 5, exactly the NAT’s 5 minutes: a NAT that sees no packet for 5 minutes forgets the connection, and pushes after that never arrive (chapter 23).
The extra connections on the server are actually the smaller matter. Suppose a tenth of v3’s 1,000,000 online users are in a browser, with 2.5 tabs open on average:
- connections go from 100,000 to 250,000, or from two gateways’ worth to five (chapter 22: 50,000 each normally);
- with a heartbeat every 4 minutes, from 417 heartbeats a second to 1,042.
Three more gateways are affordable. The real reasons are the things above, which users can see.
3. The fix: the whole browser is one device
A browser has just one device ID, one connection and one local database. One tab leads: it holds the connection and writes the local database; the other tabs only display, and go to it for everything else.
- One device ID. Generated on first open, stored in the browser, shared by all tabs. The server sees one device.
- Pick a leader. Every tab asks for the same lock (Web Locks); whichever gets it leads, and the others wait in line. When the leader tab closes, the lock is released with it (spec section 2.6), and the first in line takes over and reconnects: three round trips on Wi-Fi, about 60 milliseconds, then one sync.
- Tabs pass messages to each other. When the leader gets a push, it tells the other tabs over a BroadcastChannel; when another tab sends a message, it goes to the leader the same way.
- IndexedDB as the local database. It is chapter 7’s local database, swapped for the one built into the browser. But it must be treated as a cache: if a Safari user has not interacted with the site in seven days, Safari deletes the storage its scripts wrote, IndexedDB included (WebKit). Once it is gone, the client syncs again as if freshly installed (chapter 11); the records of 200 conversations are about 7.8 KB (39 bytes a row, chapter 14).
- Report a read only when it is seen. The read position is reported only while the page is visible (Page Visibility) and the conversation is on screen. During that hour of meeting, the hiking group’s 62 messages stay unread.
- The server sends the heartbeat. Chapter 23’s heartbeat is an application frame sent by the client, and in a background tab it relies on exactly the timers that get slowed. For browsers, the server sends a WebSocket ping instead and the browser answers with a pong itself (RFC 6455 section 5.5.2), without involving the page; a page can neither see nor send a ping (WHATWG). When a tab becomes visible again, the leader checks in and syncs once.
- One foreground state, one notification. If any tab is visible, the device counts as in the foreground; if none is, the leader reports “background” as in chapter 17. The leader pops the notifications, using the conversation as the notification’s
tag, so a new notification for the same conversation replaces the old one (MDN); the web push described below uses the sametag, so the two do not double up.
Two more things differ from the phone:
- Fall back to long polling when WebSocket cannot connect. Company proxies, personal firewalls and antivirus software can all block WebSocket; that is exactly why Socket.IO starts with long polling and then upgrades (docs). Long polling is chapter 1’s polling, except that when the server has no new messages it holds the reply until it has some.
- When every tab is closed, use web push. Browser push is received by a service worker the page registered, even when the page is not open (MDN). iOS supports it only from 16.4, and only for web apps added to the home screen (WebKit). It is the web’s counterpart to the phone’s OS pushOS push系统推送A notification sent through Apple’s APNs, Google’s FCM or a phone maker’s channel to a phone with no live connection. A third party, best effort, with no guarantee.See the glossary (chapter 17).
4. The cost
- One more layer of coordination. Picking a leader and passing messages between tabs are new code, with new bugs. Say the leader happens to be a background tab and the browser freezes it: a frozen page runs no timers and no callbacks (Page Lifecycle), and every tab stops with it. Another way is to put the connection in a SharedWorker, which belongs to no single tab; but Chrome on Android did not support it until version 148 (MDN’s compatibility data).
- Upgrading the local database waits for old tabs. A new version that changes IndexedDB’s schema is blocked while tabs running the old version still have the database open (blocked event). Old tabs must close the database when told the version is changing, and ask the user to reload.
- Background tabs cannot rely on time. Timers get slowed and pages get frozen; anything of the form “do this after so long” has to be checked again when the page becomes visible.
- The web client is most likely a separate codebase. Whether the phone’s core can run in the browser as it is, is chapter 30’s question.
5. Other answers
- A separate client in each tab. Simplest, and what most websites do; the cost is the list in section 2.
- A SharedWorker holds the connection. No leader to pick; cleaner in the browsers that support it. Telegram’s web app WebK (an open-source client listed on telegram.org/apps) does this: its MTProto networking code runs in a SharedWorker that all tabs share (apiManagerProxy.ts); where the browser has no SharedWorker, only one tab is online and the others stop (singleInstance.ts).
- Keep no history. A light client such as a support widget fetches the latest 50 messages each time a conversation opens, about 10 KB (chapter 7).
- Mini programs and web pages inside apps. Both connection and storage are limited by the host, and every host has its own rules.
6. This chapter’s decision
On the client side, both the phone and the browser are now settled. Back to the server: at 13,900 messages a second, neither a single message server nor a single storage node can keep up. Next: sharing out the conversations.
Comments
Comments live in GitHub Discussions and need a GitHub account.