A named message bus
The BroadcastChannel API lets browsing contexts of the same origin send messages to each other. Any tab, iframe, or worker that opens a channel with the same name joins a shared bus and receives every message posted to it.
- Create a channel by name to join the bus.
- postMessage sends to all other subscribers, not the sender.
- A message event delivers the data, which is structured cloned.
When to reach for it
It shines for coordinating state across tabs without a server round trip.
- Notify other tabs that the user logged out everywhere.
- Sync a theme or settings change instantly.
- Tell siblings that cached data was refreshed.
The sender never receives its own message, so each context updates its own state locally and broadcasts the change for others. Messages stay within one origin, which keeps the channel a private coordination tool rather than a cross site one. Remember to close the channel when a context unloads to release it.
Key idea
BroadcastChannel is a same origin named bus where posting reaches every other context but never the sender, ideal for syncing state across tabs.