BroadcastChannel
The BroadcastChannel interface represents a named channel that any browsing context of a given origin can subscribe to. It allows communication between different documents (in different windows, tabs, frames or iframes) of the same origin. Messages are broadcasted via a message event fired at all BroadcastChannel objects listening to the channel, except the object that sent the message.
EventTarget BroadcastChannel
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 54 | 79 | 38 | 15.4 | 54 | 15.4 | |
| The BroadcastChannel() constructor creates a new BroadcastChannel and connects it to the underlying channel. | 54 | 79 | 38 | 15.4 | 54 | 15.4 |
| The close() method of the BroadcastChannel interface terminates the connection to the underlying channel, allowing the object to be garbage collected. This is a necessary step to perform as there is no other way for a browser to know that this channel is not needed anymore. | 54 | 79 | 38 | 15.4 | 54 | 15.4 |
| The message event of the BroadcastChannel interface fires when a message arrives on that channel. | 54 | 79 | 38 | 15.4 | 54 | 15.4 |
| The messageerror event of the BroadcastChannel interface fires when a message that can't be deserialized arrives on the channel. | 60 | 79 | 57 | 15.4 | 60 | 15.4 |
| The name read-only property of the BroadcastChannel interface returns a string, which uniquely identifies the given channel with its name. This name is passed to the BroadcastChannel.BroadCastChannel constructor at creation time and is therefore read-only. | 54 | 79 | 38 | 15.4 | 54 | 15.4 |
| The postMessage() method of the BroadcastChannel interface sends a message, which can be of any kind of Object, to each listener in any browsing context with the same origin. The message is transmitted as a BroadcastChannel/message_event event targeted at each BroadcastChannel bound to the channel. | 54 | 79 | 38 | 15.4 | 54 | 15.4 |
Use cases
-
Using BroadcastChannel
The BroadcastChannel interface represents a named channel that any browsing context of a given origin can subscribe to.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.