Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

How do tabs of the same site talk to each other?

Questions in this chapter (5)
  • Draft
  • medium
  • Askedoften
  • Verified October 2026

Answer

Tabs of one origin share no JavaScript objects, so they coordinate through the browser:

BroadcastChannel is a named pub/sub bus. new BroadcastChannel('auth') in each tab, postMessage(data) in one, a message event in all the others. Data is structured-cloned, like postMessage() to a worker. It reaches every other channel object with that name in the same origin, including workers and a second channel object in the sending tab; only the object that sent it is skipped. Matching uses the storage key, so a third-party iframe only reaches copies of itself embedded under the same top-level site.

SharedWorker is one worker shared by every same-origin page that creates it with the same URL and name. Each page talks to it through its own MessagePort (worker.port), and the worker gets a connect event per page. Use it for shared state or one shared connection, such as a single WebSocket for all tabs. It runs while any page is connected. Chrome Android shipped it only in Chrome 148.

Web Locks coordinate rather than message. navigator.locks.request('sync', async (lock) => {…}) runs the callback once no other tab or worker of the origin holds 'sync', and releases the lock when the returned promise settles or the tab closes. { ifAvailable: true } gives null instead of waiting; { steal: true } takes the lock and rejects the holder's request with AbortError; mode: 'shared' allows several readers.

Others: the storage event fires in other tabs when localStorage changes; window.opener and iframes can postMessage each other directly; a service worker can message every client with clients.matchAll().

Pick by need: notifications → BroadcastChannel; one owner of a resource → a lock, or a SharedWorker where supported.

Example

// Every tab of the app runs this
const channel = new BroadcastChannel('auth');
channel.onmessage = (event) => {
  if (event.data.type === 'logout') console.log(`logged out in another tab: ${event.data.reason}`);
};

function logout(reason) {
  // …clear the session locally, then tell the other tabs
  channel.postMessage({ type: 'logout', reason });
}

// In tab A:
logout('user');
// → logged out in another tab: user

Tabs B and C each printed the line, and a dedicated worker in tab C listening on 'auth' got the message too; tab A printed nothing. A tab on http://127.0.0.1:8070 with the same code got nothing either, since it's another origin. Same in Chrome 156 and Firefox 159, driven with Puppeteer.

Pitfalls

  • Expecting the sender to receive its own message. Update the sending tab directly; the channel is for the others.
  • Assuming steal stops the old holder. Its request rejects with AbortError, but its callback keeps running; code inside must check whether it still owns the work.
  • Relying on SharedWorker everywhere. Safari dropped it after version 7 and restored it in 16; Chrome Android got it in 148.
  • Leaving channels open on pages you want back-forward cached. Chrome 156 cached a page with an open channel, but a message sent while it was cached evicted it.

Follow-ups

How do you elect one leader tab?
Every tab calls navigator.locks.request('leader', () => new Promise(() => {})). The first holds the lock forever; the rest queue. When the leader's tab closes, the browser releases its lock and the next tab's callback runs. Chrome 156 and Firefox 159 granted the waiting tab the lock when the holder closed.
BroadcastChannel or the storage event?
The storage event only reports localStorage writes, as strings, and only when the value changes. BroadcastChannel sends any cloneable value without storing it. Use storage when the state must also survive a reload.
Do messages reach a page in the back-forward cache?
No. The spec delivers only to documents that are fully active. Headless Firefox didn't restore pages from the cache under Puppeteer, so only Chrome was checked.

Exercises

What does tab B print?

// Tab A
navigator.locks.request('sync', async () => {
  await new Promise((resolve) => setTimeout(resolve, 1000));
});

// Tab B, 100 ms later
navigator.locks.request('sync', { ifAvailable: true }, async (lock) => {
  console.log(lock === null);
});

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.