Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Browser and Web APIs

The BroadcastChannel object

Reference for BroadcastChannel: sending messages between tabs, windows, iframes and workers of the same origin, with postMessage(), onmessage and close().

A BroadcastChannel is a named channel that every same-origin browsing context can join: other tabs, windows, iframes and workers. A message posted on the channel is delivered to all other BroadcastChannel objects with the same name. Typical uses are signing out of every tab at once, syncing a theme choice or shopping cart, and telling other tabs that data has changed.

The channel is one-to-many and has no server. The sender never receives its own message, but another channel object with the same name in the same page does, which makes it easy to test.

JavaScript
const channel = new BroadcastChannel("app");
channel.postMessage({ type: "logout" });
channel.onmessage = (event) => console.log(event.data);

Try it

JS Two channel objects, one name

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Keeping state in sync

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
const channel = new BroadcastChannel(name)
channel.postMessage(message)
channel.close()
channel.onmessage = (event) => { /* event.data */ }
channel.onmessageerror = (event) => { /* could not deserialize */ }
Name Description
name The channel name, a string. Contexts that use the same name (and the same origin) can talk to each other.
message Any value the structured clone algorithm supports. Functions, DOM nodes and objects with methods that are not plain data cannot be sent.

Properties: name (read-only). Events: message and messageerror; the MessageEvent has data and origin.

Return value: postMessage() returns undefined. Exceptions: postMessage() throws DataCloneError when the value cannot be cloned and InvalidStateError on a closed channel.

Usage notes

  • Same origin only. Scheme, host and port must match. For other origins use window.postMessage() with an origin check.
  • No echo to the sender. Update your own tab directly, and broadcast to the others, as in the second example.
  • No guaranteed history. Tabs that open later do not receive old messages. Combine with localStorage or a server for lasting state.
  • No delivery guarantee. If a tab is frozen or closed, it misses messages.
  • Messages are copies. The receiver gets a clone of the data.
  • Close unused channels. An open channel keeps the object alive, and in Node.js can keep the process running.
  • Alternatives. The storage event also tells tabs about localStorage changes; SharedWorker allows a shared central state; Web Locks help when tabs must take turns.

Browser support

BroadcastChannel is supported in all current browsers. Partitioned storage in newer browsers can stop it from crossing between a third-party iframe and a top-level page of the same site, so check a compatibility table for embedded use.