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
BroadcastChannelinstance 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 pollinglocalStorageor 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 realDOMException— a channel cannot be reused after closing. - A precise answer names the real, practical contrast with polling
localStoragefor 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;BroadcastChannelis 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.