The MessagePort
interface of the Channel Messaging API represents one of the two ports of a MessageChannel
, allowing messages to be sent from one port and listening out for them arriving at the other.
Inherits methods from its parent, EventTarget
MessagePort.postMessage
MessagePort.start
EventTarget.addEventListener
; it is implied when using MessagePort.onmessage
.)MessagePort.close
Inherits event handlers from its parent, EventTarget
MessagePort.onmessage
EventListener
called when a MessageEvent
of type message
is fired on the port—that is, when the port receives a message.MessagePort.onmessageerror
EventListener
called when a MessageEvent
of type MessageError
is fired—that is, when it receives a message that cannot be deserialized.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 (run it live too).
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Message ports' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 4 | Yes | Yes | 10 | 10.6 | 5 |
Available in workers | Yes | ? | 41 | Yes | Yes | Yes |
close |
4 | Yes | Yes | 10 | 10.6 | 5 |
onmessage |
4 | Yes | Yes | 10 | 10.6 | 5 |
onmessageerror |
60 | ? | 57 | ? | 47 | ? |
postMessage |
4 | Yes | Yes | 10 | 10.6 | 5 |
start |
4 | Yes | Yes | 10 | 10.6 | 5 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | 18 | Yes | Yes | 11.5 | 5.1 | ? |
Available in workers | Yes | Yes | ? | 41 | Yes | Yes | Yes |
close |
Yes | 18 | Yes | No | 11.5 | 5.1 | ? |
onmessage |
Yes | 18 | Yes | No | 11.5 | 5.1 | ? |
onmessageerror |
60 | 60 | ? | 57 | 47 | ? | ? |
postMessage |
Yes | 18 | Yes | No | 11.5 | 5.1 | ? |
start |
Yes | 18 | Yes | No | 11.5 | 5.1 | ? |
© 2005–2018 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/MessagePort