Imagine you log out of an app in one tab.
You switch to another tab. Same app. Still logged in, apparently.
You click Edit. The form opens. You type. You click Save.
And only then does the server answer with a 401, and your changes are gone.
No line of code is wrong, exactly. Each tab is living in its own little world, and nobody told the second tab that the party was over.
The browser has plenty of ways to fix that. Eight of them, by my count, and from a distance they all look alike. This article is the map: what each one does, who it talks to, and when I'd reach for it.
(Spoiler: for the logout, it's BroadcastChannel. But it isn't always the answer.)
1. One question sorts them all: who is talking to whom?
Line these APIs up and they look like a pile of things that all send messages. The pile gets much smaller once you ask one question: who is on each end of the conversation?
Once you know who needs to hear the message, you're usually down to one or two candidates.
Start with the audience, and the right API mostly picks itself.
2. What they have in common
-
They're event-driven. You never ask "any news?". You add a listener and wait for a
message(orstorage) event. Your code reacts instead of polling. -
Most are same-origin only. BroadcastChannel, SharedWorker, Service Worker and the storage APIs all stay inside one origin. Only
postMessage, WebSocket and SSE can cross origins, which is why they come with origin checks or CORS attached. - The local ones never leave the device. A BroadcastChannel message in your laptop's browser will never reach your phone. For that you need a server in the middle.
-
Structured clone vs. strings only.
postMessageon channels, workers and windows uses the structured clone algorithm, so objects, arrays,Map,DateandBlobarrive as real copies. Storage, WebSocket text frames and SSE give you strings, so get comfortable withJSON.stringify.
That last point is where most surprises come from. Functions and DOM nodes can't be cloned, and trying throws a DataCloneError. The browser is happy to copy your data, just not your entire app.
Same origin and same device cover most cases. Everything else needs a server or an explicit origin check.
3. The cast, one by one
Here's each one, with a few lines of code and the job it's good at.
BroadcastChannel: the named radio station
Everyone who tunes in to the same channel name hears every message. The sender doesn't hear itself, and nothing is stored, so a tab that opens later misses everything said before it arrived.
Use it for: keeping same-origin tabs in sync. Logout, theme changes, "this item was updated", "this notification was read".
window.postMessage: the direct phone line
You call one specific window, iframe or popup. It's the only local option that works across origins, so it comes with two rules: always pass an exact targetOrigin when you send, and always check event.origin when you receive. Skip the check, and any page that gets hold of your window can send you whatever it likes. It will.
Use it for: iframes, popups (OAuth and payment windows are the classics) and embedded widgets from another origin.
SharedWorker: the roommate who holds the one connection
A SharedWorker is a script that runs in the background, separate from any page, and every tab of your site connects to the same copy of it. It stays alive as long as at least one of those tabs is open, and it remembers whatever you keep in its variables the whole time. Each tab talks to it through its own port, and the worker keeps a list of them. The classic use: the worker opens one WebSocket and passes every message on to all the tabs.
On the page side, it's new SharedWorker('/cart-worker.js').port.onmessage = .... Two catches. A closed tab doesn't tell the worker it left, so a real version has to clean up dead ports. And mobile support has been patchy for years: Chrome for Android only caught up recently, and some mobile browsers still don't have it. Check your audience before you rely on it.
Use it for: one expensive resource (a socket, a big cache) shared by every tab.
Service Worker: the bouncer who naps
A Service Worker is also a background script, but its real job is the network. Every request from the pages it controls goes through it first, and it decides whether to go to the server or answer from a cache. That's how offline mode works. It can even wake up when no tab is open at all, for example to receive a push notification. Talking to tabs is a side feature: clients.matchAll() lists the open pages it controls, and you postMessage each one.
The catch: the browser stops it whenever it's idle and starts it again for the next event. Anything you keep in a variable is gone after the nap. A goldfish with a very good network connection. So state belongs in IndexedDB or the Cache API, not in memory.
Pages listen with navigator.serviceWorker.onmessage.
So the difference in one sentence: a SharedWorker lives with your open tabs and remembers, while a Service Worker lives with your site, works even with no tabs open, and forgets.
Use it for: push notifications, offline sync, and anything that has to happen while no page is in focus.
localStorage and sessionStorage: the shelf (and its doorbell)
These are storage, not messaging. Strings only, about 5 MB per origin, and every read and write is synchronous. localStorage is shared by all tabs and survives a browser restart. sessionStorage belongs to one tab. A tab opened from it (or duplicated, in most browsers) starts with a copy, and from then on the two go their separate ways.
The shelf does have a doorbell, though. When localStorage changes, every other same-origin tab gets a storage event. Before BroadcastChannel, that was the trick for cross-tab messages: write a key, and the other tabs react. It fires only in the other tabs, never in the one that made the change. It still works fine. It just needs a lot more paperwork than the job requires.
Use it for: the last-known state that should still be there after a reload. The doorbell is a free bonus when you already write the value there, or a fallback for very old browsers. For plain messages, BroadcastChannel is simpler.
WebSocket and SSE: the long-distance calls
Everything above stays on one device. When an update has to reach your other laptop, your phone, or another user, it has to go through a server.
WebSocket is a two-way line. SSE (EventSource) only sends from server to browser, but it reconnects automatically and runs over plain HTTP, which makes it much simpler to operate.
Use it for: chat and collaboration (WebSocket), feeds and notifications (SSE).
That's the whole cast.
Each tool is best at one job, and most bugs come from asking it to do a different one.
4. Picking one
Here's the decision list I go through. Start at the top, and the first "yes" wins.
Storage answers "where does the data live", not "who gets told". So if you land on the last box, you probably still need one of the boxes above it. Nobody has ever been notified by a shelf.
The first yes wins, and storage is never that yes on its own.
5. The real-world combo
In practice, one tool is rarely enough. A setup that shows up again and again has three parts:
-
One connection. A SharedWorker holds the WebSocket. Where SharedWorker isn't available, one tab becomes the leader and holds it instead. The Web Locks API (
navigator.locks) is a tidy way to pick that tab. - One fan-out. Whoever holds the socket passes every update on through a BroadcastChannel, so all tabs hear it at once.
-
One memory. The last-known state goes into
localStorage, so a tab that opens later, or reloads, starts from the right place instead of an empty screen.
Each part covers a gap in the others. BroadcastChannel forgets everything. localStorage remembers but never announces. The WebSocket knows about other devices, but it shouldn't be opened ten times for ten tabs. Ten tabs, ten sockets, one very confused backend.
The logout from the beginning fits right in. A logout in one tab goes out on the channel, every other tab redirects to the login page, and nobody gets to fill in a form that's already doomed.
I built the fan-out part for notifications in a real app: new notifications arrive over SSE, and when you read one, BroadcastChannel tells your other tabs so their badges update too. It gets its own article, Mark as Read, Everywhere, with the parts this overview skips: the message shape, what the sending tab does, and how to test it.
Connect once, fan out locally, and keep the last state on the shelf.
Final thoughts
Eight APIs sounds like a lot, but they come down to three ideas.
Ask who's listening. Another tab, another origin, another device. That one question removes most of the options before you write any code.
Messages and memory are different jobs. Channels and workers deliver news. Storage keeps it. Most real setups need one of each.
Local stays local. Anything that has to reach another device needs a server, no matter how well your tabs get along.
Your tabs are going to talk behind your back anyway. You might as well decide what they say.









Top comments (2)
the "messages and memory are different jobs" framing is super clean, especially pairing BroadcastChannel for fan-out with localStorage for the last-known state
have you run into any quirks with Safari or mobile browsers when falling back from SharedWorker to navigator.locks?
Thanks! I haven't shipped that fallback myself (my app is SSE plus BroadcastChannel), but the one I'd watch on mobile is a backgrounded leader that still holds the lock after its socket died. A heartbeat over the channel plus releasing the lock on
pagehidecovers most of it