Broadcast Channel API

On this page 7

The Broadcast Channel API is a browser interface that lets browsing contexts of the same origin, such as tabs, windows, iframes and workers, exchange messages through a named channel. Among Web APIs, it is the simplest standard tool for cross-tab communication: any context that opens a channel with the same name receives every message posted to it by the others.

The API is defined in the HTML Living Standard and is supported by all current major browsers, including Safari since version 15.4. It works entirely inside one browser profile on one device, so it never sends data over the network.

How the Broadcast Channel API delivers messages

A script creates a channel with new BroadcastChannel("name"), sends data with postMessage() and listens for the message event on the same object. The browser delivers each message to every other channel object with the same name and origin, but not to the sender itself. Closing a channel with close() stops delivery and releases resources.

Messages are copied with the structured clone algorithm, the same mechanism used by postMessage on workers. This allows objects, arrays, dates, maps, blobs and typed arrays, but not functions, DOM nodes or class methods. Each receiver gets its own independent copy, so changes in one tab never affect the data in another.

The table below summarizes the core members of the interface.

MemberTypePurpose
BroadcastChannel(name)ConstructorJoins or creates a channel by name
nameRead-only propertyReturns the channel name
postMessage(data)MethodSends a cloned message to other subscribers
close()MethodDisconnects this object from the channel
messageEventFires when another context posts data
messageerrorEventFires when a message cannot be deserialized
Main members of the BroadcastChannel interface

Scope, origin and storage partitioning rules

Delivery is limited by the same-origin policy: scheme, host and port must all match, so https://app.example.com cannot talk to https://example.com. Modern browsers also apply storage partitioning, which means an iframe embedded under a different top-level site joins a separate partition and cannot exchange messages with the same origin opened directly.

Private browsing windows form their own isolated context, and channels never cross browser profiles or devices. Delivery order is preserved for messages from one sender, but there is no acknowledgment, persistence or replay: a tab that opens after a message was sent will never see it.

How it compares with other cross-context techniques

The Broadcast Channel API is one of several ways to coordinate contexts, and it is best suited to one-to-many notifications within an origin. Neighboring Web APIs cover direct messaging, persistence and server communication.

TechniquePatternPersistenceTypical use
Broadcast Channel APIOne-to-many, same originNoneSync logout or theme across tabs
window.postMessageOne-to-one, cross-origin allowedNoneParent page and embedded iframe
MessageChannelOne-to-one port pairNonePrivate link between two contexts
storage eventOne-to-many via Web Storage APIYes, data stays storedLegacy cross-tab sync
SharedWorkerCentral hub scriptIn memory while openShared connection or state
WebSocket or server-sent eventsServer to clientServer sideSync across devices and users
Broadcast Channel API and alternative techniques

Before broad support for the API, developers often abused the storage event of localStorage to signal other tabs. That workaround writes data to disk, accepts only strings and leaves traces, while a broadcast channel is purpose-built, in memory and type-rich.

Practical patterns and design choices

Most production uses send small event messages rather than large payloads, typically an object with a type field and minimal data. Receivers then read authoritative state from IndexedDB, cookies or the server, which keeps the channel a signal, not a data store.

Use caseMessage contentReceiver action
Logout in all tabsType logoutClear state and redirect to sign-in
Theme or language changeNew setting valueApply the setting without reload
Cache invalidationResource keyRefetch or drop cached data
Service worker updateNew version noticePrompt the user to reload
Leader electionHeartbeat with tab IDLet one tab own a shared task
Common use cases and recommended message design

For leader election, the Web Locks API is usually more reliable than heartbeats on a channel, because the browser releases the lock automatically when a tab closes. Combining both, with locks for ownership and a channel for announcements, is a robust pattern for applications that should keep only one live connection.

Limitations, security and common mistakes

The API offers no authentication of senders beyond the origin check, so any script running on the same origin, including injected or third-party code, can read and post messages. Sensitive tokens should therefore never be broadcast, and receivers should validate message shape before acting on it.

  • Expecting the sender to receive its own message leads to missing updates; the posting tab must apply the change locally as well.
  • Forgetting to call close() in single-page applications keeps listeners alive after components unmount and can cause duplicate handling.
  • Relying on delivery to tabs opened later fails, because the channel has no history.
  • Sending huge objects costs serialization time in every receiving context and can block the main thread.
  • Assuming cross-device sync is a design error; synchronization between devices needs a server and a protocol such as WebSocket.

The simple explanation

Imagine having the same website open in several browser tabs. Normally each tab lives in its own bubble and has no idea what the others are doing. The Broadcast Channel API gives these tabs a shared walkie-talkie frequency. When one tab says something on the channel, every other tab of the same site hears it right away.

This is why signing out in one tab can sign you out everywhere, or why switching to dark mode changes all open tabs at once. The messages stay inside your browser and never travel over the internet.

How it is used in practice

A web-based email client is often open in three or four tabs at once: the inbox, a draft and a search result. When a user signs out in one tab, the other tabs used to keep showing private messages until they tried to load new data and failed.

The development team adds a channel named session. On sign-out, the active tab clears its own state and posts a logout message; every other tab receives it within milliseconds, wipes cached mail from memory and redirects to the sign-in page. The team also broadcasts a mailbox-changed signal when a message is read, so unread counters stay consistent across tabs without extra polling. The outcome is consistent session state in every tab and fewer redundant server requests.