Reference · print-friendly
One page for design reviews. Walk top to bottom; stop when a row rejects the current candidate.
| If… | Prefer… |
|---|---|
| Same-origin peers must hear a signal and act locally (logout, theme, invalidate) | BroadcastChannel |
| One process must own a scarce resource (socket, engine, shared memory-ish coordinator) | SharedWorker |
| One page CPU offload only | Dedicated Worker |
| Offline shell / fetch policy / push control plane | Service worker (+ Cache) |
| Tab-private sticky state for a page session | sessionStorage / memory — not a bus |
| Tiny durable prefs; optional crude multi-tab |
localStorage (+ storage events as legacy bus)
|
| Cross-origin messaging |
window.postMessage / MessageChannel — not BC
|
"app:auth")
{ type, … }); structured
clone only — no transfer list
onmessage / messageerror;
close() on teardown; never post after close
InvalidStateError“We use BroadcastChannel | SharedWorker | sessionStorage | SW | … because job; wire is name + local-then-post + small clone; truth is store/server; we own origin · support · no lock myth · close hygiene.”
const ch = new BroadcastChannel("app:sync");
ch.onmessage = (e) => handle(e.data);
function publish(msg) {
handle(msg); // local — sender excluded
ch.postMessage(msg);
}
// teardown: ch.close();