Lesson 0003 · ~12 minutes
One skill: run a design-review risk scan on
BroadcastChannel — when light pub/sub breaks down, which
patterns are safe, and how to verify with two tabs.
You can reject lock/leader myths, huge-payload buses, and “BC is the database” proposals in under a minute — and green-light logout / theme / invalidate with a two-tab test plan.
| Requirement | Why BC fails | Prefer |
|---|---|---|
| One shared WebSocket / engine | No shared process — only messages | SharedWorker |
| Cross-origin iframe bus | Same-origin (storage key) only |
window.postMessage / MessageChannel ports
|
| Authoritative durable state | Transient signals; no history | Server, IndexedDB, or other store |
| Tab-private wizard draft | BC is for fan-out, not isolation storage | sessionStorage / memory |
| Offline shell / fetch policy | Not a network proxy | Service worker + Cache |
| Safe multi-tab lock / leader | Best-effort notify, not consensus | Server lease or SharedWorker design |
Lesson 1 still holds: BC wins for same-origin notify when each peer can react locally (HTML §9.5 points elaborate cases at shared workers).
The posting algorithm queues tasks to current destinations that match name + partitioning, then excludes the sender (§9.5). Implications for design reviews:
“BroadcastChannel is a live bus, not a mailbox. If a tab must know history or hold a lock safely, put authority elsewhere.”
MDN marks the Broadcast Channel API as Baseline Widely available (across major engines since March 2022) (MDN). Practical floor from caniuse-class tables: Chromium (long), Firefox (long), Safari 15.4+ (macOS and iOS). Internet Explorer never shipped it.
typeof BroadcastChannel === "function"; fallback often
means
localStorage + storage events
(stringly, side-effecty) or “no multi-tab sync.”
Spec’s motivating example: local logout +
postMessage("logout"); peers clear UI. Truth still sits
in cookies/session server-side.
Apply locally, persist (localStorage/server), post small
{ type, value }. Peers apply; late tabs read storage.
“Orders list dirty” — peers re-fetch or re-read IDB. The message is a hint, not the new list body.
Optional UX: “another tab may be editing.” Never sole safety for writes without server checks.
Racing tabs both claim “I own the lock” via posts with no durable lease → lost updates. Needs server or a real coordinator process.
Structured clone only — no transfer list (lesson 2). Huge messages
cost every peer. Prefer ids and enums. Closed channels throw
InvalidStateError. Name typos fail silently.
Full steps live in integration-checklist.html. Minimum:
onmessage.close() then post → InvalidStateError.Equal-length options. Design-review voice.
Proposal: “Tabs elect a leader by posting on BroadcastChannel; the leader alone mutates the cart with no server checks.”
After saving an order in Tab A, other open tabs should refresh their orders table from the API.
User toggles theme in Tab A. Tab C opens five minutes later. How should Tab C get the current theme?
Shell on app.example.com and widget iframe on
cdn.example.com must share one named notify bus.
Product targets current Chromium, Firefox, and Safari 16+. Need multi-tab logout notify. How do you treat support?
Re-skim HTML §9.5 (simple vs elaborate cases, destination algorithm). Support posture: MDN Broadcast Channel API. Keep the checklist for reviews.