Cross-Tab & Files
Coordinating across tabs and persisting files
Section titled “Coordinating across tabs and persisting files”Modern web apps often run in multiple browser tabs simultaneously — a user may have the same dashboard open in two windows. When one tab updates data, how does the other find out? When the app needs to write a large file, how does it avoid clobbering work in a concurrent tab? This module answers both questions.
The browser gives you four complementary APIs:
- BroadcastChannel — publish/subscribe messaging across every tab (and worker) on the same origin.
storageevent — fired on other tabs whenlocalStorageis written; the original notification mechanism before BroadcastChannel.- Web Locks — a mutual-exclusion primitive that serialises access to a named resource, preventing race conditions.
- File System Access / OPFS — read and write real files (or a private origin sandbox) with a streaming, async API.
How tabs communicate
Section titled “How tabs communicate”flowchart LR
subgraph Origin["Same origin (scheme + host + port)"]
TabA["Tab A\n(writer)"] -->|postMessage| BC["BroadcastChannel\n'app-updates'"]
BC -->|onmessage| TabB["Tab B\n(listener)"]
BC -->|onmessage| TabC["Tab C\n(listener)"]
TabA -->|setItem| LS[(localStorage)]
LS -->|storage event| TabB
LS -->|storage event| TabC
end What this module covers
Section titled “What this module covers”| Lesson | What you will learn |
|---|---|
| This page | The four cross-tab and file APIs and when to reach for each one |
broadcastchannel | new BroadcastChannel(name), postMessage, onmessage, close; vs the storage event |
web-locks | navigator.locks.request, exclusive vs shared mode, ifAvailable, locks.query() |
cross-tab-state | Combining BroadcastChannel + localStorage/IndexedDB for shared state; leader election with Web Locks |
file-system-access-opfs | showOpenFilePicker / showSaveFilePicker (user gesture required) and OPFS navigator.storage.getDirectory() |
Quick comparison
Section titled “Quick comparison”| API | Delivery | Requires gesture | Works in workers | Persistent |
|---|---|---|---|---|
| BroadcastChannel | other tabs | no | yes | no |
storage event | other tabs | no | no | no |
| Web Locks | same tab | no | yes | no |
| File System Access | — | yes (picker) | no | yes |
| OPFS | — | no | yes | yes |
Your first cross-tab message
Section titled “Your first cross-tab message”The snippet below creates a BroadcastChannel and immediately posts and receives a message in the same page. In a real app the sender and receiver are in separate tabs.