The Channel Messaging API allows two separate scripts running in different browsing contexts attached to the same document (e.g., two IFrames, or the main document and an IFrame, two documents via a SharedWorker
, or two workers) to communicate directly, passing messages between one another through two-way channels (or pipes) with a port at each end.
A message channel is created using the MessageChannel()
constructor. Once created, the two ports of the channel can be accessed through the MessageChannel.port1
and MessageChannel.port2
properties (which both return MessagePort
objects.) The app that created the channel uses port1
, and the app at the other end of the port uses port2
— you send a message to port2
, and transfer the port over to the other browsing context using window.postMessage
along with two arguments (the message to send, and the object to transfer ownership of, in this case the port itself.)
When these transferable objects are transferred, they are 'neutered' on the previous context — the one they previously belonged to. For instance a port, when is sent, cannot be used anymore by the original context. Note that the only two objects that can currently be transferred are ArrayBuffer
and MessagePort
.
The other browsing context can listen for the message using MessagePort.onmessage
, and grab the contents of the message using the event's data
attribute. You could then respond by sending a message back to the original document using MessagePort.postMessage
.
When you want to stop sending messages down the channel, you can invoke MessagePort.close
to close the ports.
Find out more about how to use this API in Using channel messaging.
MessageChannel
MessagePort
PortCollection
MessagePort
s; an experimental solution to allow broadcasting of a message to multiple ports simultaneously.<iframe>
.Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard | Living Standard | Channel messaging defined in section 9.5. No difference to the the HTML5 Web Messaging spec. |
HTML5 Web Messaging | Recommendation | W3C version of the spec. |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 4 | 41 (41) | 10.0 | 10.6 | 5 |
PortCollection | No support | No support | No support | No support | No support |
Available in workers | (Yes) | 41 (41) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 4.4 | 4 | 41.0 (41) | No support | 10.0 | 11.5 | 5.1 |
PortCollection | No support | No support | No support | No support | No support | No support | No support |
Available in workers | (Yes) | (Yes) | 41.0 (41) | (Yes) | (Yes) | (Yes) | (Yes) |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API