Channel Messaging API
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.
Note: This feature is available in Web Workers
Channel messaging concepts and usage
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 no longer usable on the context they previously belonged to. A port, after it is sent, can no longer be used by the original context.
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.
Channel messaging interfaces
MessageChannel
-
Creates a new message channel to send messages across.
MessagePort
-
Controls the ports on the message channel, allowing sending of messages from one port and listening out for them arriving at the other.
PortCollection
-
An array of
MessagePort
s; an experimental solution to allow broadcasting of a message to multiple ports simultaneously.
Examples
- We have published a channel messaging basic demo on Github (run it live too), which shows a really simple single message transfer between a page and an embedded
<iframe>
. - You can also see a multimessaging demo (run this live), which shows a slightly more complex setup that can send multiple messages between main page and IFrame.
Specifications
Specification |
---|
HTML Standard (HTML) # message-channels |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Channel_Messaging_API |
4 |
12 |
41 |
10 |
10.6 |
5 |
≤37 |
18 |
41 |
11 |
4.2 |
1.0 |
close |
4 |
12 |
41 |
10 |
10.6 |
5 |
≤37 |
18 |
41 |
11 |
4.2 |
1.0 |
message_event |
4 |
12 |
Yes |
10 |
10.6 |
5 |
≤37 |
18 |
No |
11.5 |
4.2 |
1.0 |
messageerror_event |
60 |
18 |
57 |
No |
47 |
No
See bug 171216.
|
60 |
60 |
57 |
47 |
No
See bug 171216.
|
8.0 |
onmessage |
4 |
12 |
41 |
10 |
10.6 |
5 |
≤37 |
18 |
41 |
11 |
4.2 |
1.0 |
onmessageerror |
60 |
18 |
57 |
No |
47 |
No
See bug 171216.
|
60 |
60 |
57 |
44 |
No
See bug 171216.
|
8.0 |
postMessage |
4 |
12 |
41 |
10 |
10.6 |
5 |
≤37 |
18 |
41 |
11 |
4.2 |
1.0 |
start |
4 |
12 |
41 |
10 |
10.6 |
5 |
≤37 |
18 |
41 |
11 |
4.2 |
1.0 |
worker_support |
4 |
12 |
41 |
10 |
≤15 |
5 |
≤37 |
18 |
41 |
≤14 |
5 |
1.0 |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Channel_Messaging_API |
4 |
12 |
41 |
10 |
10.6 |
5 |
4.4 |
18 |
41 |
11 |
4.2 |
1.0 |
MessageChannel |
4 |
12 |
41 |
10 |
10.6 |
5 |
4.4 |
18 |
41 |
11 |
4.2 |
1.0 |
port1 |
4 |
12 |
41 |
10 |
10.6 |
5 |
4.4 |
18 |
41 |
11 |
4.2 |
1.0 |
port2 |
4 |
12 |
41 |
10 |
10.6 |
5 |
4.4 |
18 |
41 |
11 |
4.2 |
1.0 |
BCD tables only load in the browser
MessagePort
BCD tables only load in the browser
See also
© 2005–2021 MDN 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