1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
---
title: BroadcastChannel
slug: Web/API/BroadcastChannel
tags:
- API
- Broadcast Channel API
- Experimental
- HTML API
- Interface
- Reference
translation_of: Web/API/BroadcastChannel
---
<p>{{APIRef("Broadcast Channel API")}}</p>
<p>The <code><strong>BroadcastChannel</strong></code> 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 <code>BroadcastChannel</code> objects listening to the channel.</p>
<p>{{AvailableInWorkers}}</p>
<h2 id="Constructor">Constructor</h2>
<dl>
<dt>{{domxref("BroadcastChannel.BroadcastChannel", "BroadcastChannel()")}}</dt>
<dd>Creates an object linking to the named channel.</dd>
</dl>
<h2 id="Properties">Properties</h2>
<p><em>This interface also inherits properties from its parent, {{domxref("EventTarget")}}.</em></p>
<dl>
<dt>{{domxref("BroadcastChannel.name")}}</dt>
<dd>Returns a {{domxref("DOMString")}}, the name of the channel.</dd>
<dt>
<h3 id="Event_handlers">Event handlers</h3>
</dt>
<dt>{{domxref("BroadcastChannel.onmessage")}}</dt>
<dd>An {{domxref("EventHandler")}} property that specifies the function to execute when a {{event("message")}} event is fired on this object.</dd>
<dt>{{domxref("BroadcastChannel.onmessageerror")}}</dt>
<dd>An {{domxref("EventHandler")}} called when a {{domxref("MessageEvent")}} of type {{domxref("MessageError")}} is fired—that is, when it receives a message that cannot be deserialized.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<p><em>This interface also inherits methods from its parent, {{domxref("EventTarget")}}.</em></p>
<dl>
<dt>{{domxref("BroadcastChannel.postMessage()")}}</dt>
<dd>Sends the message, of any type of object, to each <code>BroadcastChannel</code> object listening to the same channel.</dd>
<dt>{{domxref("BroadcastChannel.close()")}}</dt>
<dd>Closes the channel object, indicating it won't get any new messages, and allowing it to be, eventually, garbage collected.</dd>
</dl>
<h2 id="Events">Events</h2>
<dl>
<dt><code><a href="/en-US/docs/Web/API/BroadcastChannel/message_event">message</a></code></dt>
<dd>Fired when when a message arrives on the channel.<br>
Also available via the <code><a href="/en-US/docs/Web/API/BroadcastChannel/onmessage">onmessage</a></code> property.</dd>
<dt><code><a href="/en-US/docs/Web/API/BroadcastChannel/messageerror_event">messageerror</a></code></dt>
<dd>Fired when a message arrives that can't be deserialized.<br>
Also available via the <code><a href="/en-US/docs/Web/API/BroadcastChannel/onmessageerror">onmessageerror</a></code> property.</dd>
</dl>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', "comms.html#broadcastchannel", "BroadcastChannel")}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.BroadcastChannel")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>Another, more heavyweight, way of communicating between browser contexts: {{domxref("ServiceWorker")}}.</li>
<li><a href="/en-US/docs/Web/API/Broadcast_Channel_API">Broadcast Channel API overview</a></li>
</ul>
|