---
title: BroadcastChannel
slug: Web/API/BroadcastChannel
tags:
- API
- Broadcast Channel API
- Experimental
- HTML API
- Interface
- Reference
translation_of: Web/API/BroadcastChannel
---
{{APIRef("Broadcast Channel API")}}
The BroadcastChannel
interface represents a named channel that any {{glossary("browsing context")}} of a given {{glossary("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 {{event("message")}} event fired at all BroadcastChannel
objects listening to the channel.
{{AvailableInWorkers}}
Constructor
- {{domxref("BroadcastChannel.BroadcastChannel", "BroadcastChannel()")}}
- Creates an object linking to the named channel.
Properties
This interface also inherits properties from its parent, {{domxref("EventTarget")}}.
- {{domxref("BroadcastChannel.name")}}
- Returns a {{domxref("DOMString")}}, the name of the channel.
-
Event handlers
- {{domxref("BroadcastChannel.onmessage")}}
- An {{event("Event_handlers", "event handler")}} property that specifies the function to execute when a {{event("message")}} event is fired on this object.
- {{domxref("BroadcastChannel.onmessageerror")}}
- An {{event("Event_handlers", "event handler")}} called when a {{domxref("MessageEvent")}} of type {{domxref("MessageError")}} is fired—that is, when it receives a message that cannot be deserialized.
Methods
This interface also inherits methods from its parent, {{domxref("EventTarget")}}.
- {{domxref("BroadcastChannel.postMessage()")}}
- Sends the message, of any type of object, to each
BroadcastChannel
object listening to the same channel.
- {{domxref("BroadcastChannel.close()")}}
- Closes the channel object, indicating it won't get any new messages, and allowing it to be, eventually, garbage collected.
Events
message
- Fired when when a message arrives on the channel.
Also available via the onmessage
property.
messageerror
- Fired when a message arrives that can't be deserialized.
Also available via the onmessageerror
property.
Specifications
Specification |
Status |
Comment |
{{SpecName('HTML WHATWG', "comms.html#broadcastchannel", "BroadcastChannel")}} |
{{Spec2('HTML WHATWG')}} |
Initial definition. |
Browser compatibility
{{Compat("api.BroadcastChannel")}}
See also