BroadcastChannel and Web Locks API
BroadcastChannel delivers structured-cloned messages to every same-origin tab, iframe, and worker subscribed to a channel name. navigator.locks.request() adds an async mutex managed by the browser process that automatically releases when the callback's Promise settles or the tab crashes.
Cross-Tab Leader Election via Web Locks + BroadcastChannel
Watch Tab 1 hold an exclusive lock while Tab 2 queues behind it and receives state updates.
Tab 1 calls navigator.locks.request('sync-leader', fn) and holds the lock while its Promise remains pending.
navigator.locks.request('sync-leader', async () => { /* leader */ });- tab1Role
- LEADER
- tab2Role
- QUEUED
- lockHolder
- Tab 1
Under the Hood
localStorage storage events were a leaky bulletin board; BroadcastChannel is a dedicated intercom, and navigator.locks is the physical keycard that ensures only one tab writes to OPFS or IndexedDB at a time.
- Origin Partitioning: Both APIs are partitioned by
(origin, top-level site)in modern storage-partitioned browsers, blocking cross-site tracking. - Sender Exclusion:
channel.postMessage(msg)dispatchesmessageevents to other contexts on the channel, never back to the sender instance. - Crash-Safe Leases: Because the browser process owns the lock table, killing a tab's renderer process immediately releases its held locks to the next queued waiter.
const bus = new BroadcastChannel('auth-sync');
async function refreshSessionOnce() {
await navigator.locks.request('auth-refresh', { ifAvailable: true }, async (lock) => {
if (!lock) return; // Another tab already holds the refresh lock
const token = await fetch('/api/refresh', { method: 'POST' }).then((r) => r.text());
bus.postMessage({ type: 'TOKEN_UPDATED', token });
});
}Problem Genealogy
- 2009 (
localStoragestorageevent): Synchronous string-only cross-tab hack that failed inside workers and stalled the main thread. - 2016 (
BroadcastChannel): Added structured-cloned async pub/sub across windows and workers. - 2022 (
Web Locks API): Replaced brittlelocalStoragetimestamp heartbeats with kernel-style async mutexes.
Production Pitfalls
- Holding a lock forever for leader election: If a leader tab sleeps in bfcache while holding a lock, it can stall other tabs unless you release locks on
pagehideor useAbortController. - Nested lock deadlocks: Acquiring lock
AthenBin one tab andBthenAin another hangs both queues permanently; pass anAbortSignaltimeout.
- When not to use
BroadcastChannel: Cross-origin iframes cannot share aBroadcastChannel; usewindow.postMessage()with explicittargetOriginchecks instead.
Connections & Logical Next Steps
navigator.locks provides browser-managed mutual exclusion while BroadcastChannel fans out leader state to followers.
BroadcastChannel messages and Web Lock grants arrive as asynchronous tasks on each context's event loop.
Web Locks prevent NoModificationAllowedError when multiple tabs open the same OPFS file.
IndexedDB locks object stores per transaction, whereas Web Locks and BroadcastChannel coordinate multi-tab state explicitly.
Seen in Architecture Autopsies
Two open tabs wake up simultaneously on reconnect and race to read and mutate the same IndexedDB outbox.
Coordinates cross-tab socket ownership using Web Locks and fans out frames over BroadcastChannel.