Skip to the solution
MediumFrontend6 min read

How would you use the BroadcastChannel API to sync application state across multiple open browser tabs?

1.1k views

The problem

Question presented to candidate: "A user logs out in one browser tab. You need every OTHER open tab of your app to also immediately reflect the logged-out state. How would you notify them, without polling localStorage or a server round-trip?"

What a strong answer should cover:

  • πŸ“Œ Interview term: BroadcastChannel β€” a real, built-in browser API letting any number of same-origin browsing contexts (tabs, windows, iframes, even workers) construct a channel with the SAME name string and genuinely send/receive messages between each other directly, entirely client-side.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: a message posted on one BroadcastChannel instance is genuinely delivered to every OTHER instance constructed with the same channel name (new BroadcastChannel("app-state")) β€” the real mechanism to notify sibling tabs of a logout without polling localStorage or hitting a server.
  • πŸ“Œ Interview term: the real, sharp, self-exclusion fact β€” verified directly: a channel genuinely does NOT receive its own posted message β€” a message handler registered on the SAME instance that called .postMessage() never fires for that message, confirmed directly by an empty self-receive log even while a SEPARATE instance on the identical channel name correctly received it.
  • πŸ“Œ Interview term: .close() β€” verified directly: calling .close() on a channel and then attempting .postMessage() on it genuinely throws a real DOMException β€” a channel cannot be reused after closing.
  • A precise answer names the real, practical contrast with polling localStorage for the "storage" event: that older pattern genuinely works too (and fires cross-tab, also correctly excluding the tab that made the write) but requires structuring the "message" as a stored key-value pair; BroadcastChannel is the more direct, purpose-built, message-passing-shaped tool for this exact use case.

Clarifying questions expected:

  • None β€” this is a definitional/practical question; directly demonstrating the real, verified cross-instance delivery and self-exclusion behavior is the strong signal.

Code / implementation expected: Yes β€” real, separate BroadcastChannel instances on the same channel name, verified with cross-instance delivery, the self-exclusion behavior, and the closed-channel error.

  • messaging
  • tricky
  • real-world

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: BroadcastChannel genuinely delivers messages between separate instances on the same channel name, but a channel never receives its own posted message β€” verified directly

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 46 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How do you convert a callback-based function to a Promise?

Continue