The MessageChannel
interface of the Channel Messaging API allows us to create a new message channel and send data through it via its two MessagePort
properties.
Note: This feature is available in Web Workers.
Properties
MessageChannel.port1
Read only- Returns port1 of the channel.
MessageChannel.port2
Read only- Returns port2 of the channel.
Constructor
MessageChannel()
- Returns a new
MessageChannel
object with two newMessagePort
objects.
Example
In the following example, you can see a new channel being created using the MessageChannel.MessageChannel
constructor.
When the IFrame has loaded, we register an onmessage
handler for MessageChannel.port1
and transfer MessageChannel.port2
to the IFrame using the window.postMessage
method along with a message.
When a message is received back from the IFrame, the onMessage
function simply outputs the message to a paragraph.
var channel = new MessageChannel();
var output = document.querySelector('.output');
var iframe = document.querySelector('iframe');
// Wait for the iframe to load
iframe.addEventListener("load", onLoad);
function onLoad() {
// Listen for messages on port1
channel.port1.onmessage = onMessage;
// Transfer port2 to the iframe
iframe.contentWindow.postMessage('Hello from the main page!', '*', [channel.port2]);
}
// Handle messages received on port1
function onMessage(e) {
output.innerHTML = e.data;
}
For a full working example, see our channel messaging basic demo on Github ([[../../../../../../mdn.github.io/dom-examples/channel-messaging-basic/index|run it live too]]).
Specifications
Specification | Status | Comment |
HTML Living StandardThe definition of 'Message channels' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
MessageChannel
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support 41 |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support 4.4 |
Chrome Android
Full support 18 |
Firefox Android
Full support 41 |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
MessageChannel() constructor
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support 41 |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support 4.4 |
Chrome Android
Full support 18 |
Firefox Android
Full support 41 |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
port1
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support 41 |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support 4.4 |
Chrome Android
Full support 18 |
Firefox Android
Full support 41 |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
port2
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support 41 |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support 4.4 |
Chrome Android
Full support 18 |
Firefox Android
Full support 41 |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
See also
MessageChannel by Mozilla Contributors is licensed under CC-BY-SA 2.5.