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.
Note: This feature is available in Web Workers.
Methods
Inherits methods from its parent, EventTarget
postMessage()
- Sends a message from the port, and optionally, transfers ownership of objects to other browsing contexts.
start()
- Starts the sending of messages queued on the port (only needed when using
EventTarget.addEventListener
; it is implied when usingMessagePort.onmessage
.) close()
- Disconnects the port, so it is no longer active.
Event handlers
Inherits event handlers from its parent, EventTarget
onmessage
- An
EventListener
called whenMessageEvent
of typemessage
is fired on the port—that is, when the port receives a message. onmessageerror
- An
EventListener
called when aMessageEvent
of typeMessageError
is fired—that is, when it receives a message that cannot be deserialized.
Events
message
- Fired when a
MessagePort
object receives a message. Also available via theonmessage
property. messageerror
- Fired when a
MessagePort
object receives a message that can't be deserialized. Also available via theonmessageerror
property.
Example
In the following example, you can see a new channel being created using the 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 ports' 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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
MessagePort
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
Full support Yes |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
close
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
No support No |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
message event
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
No support No |
Opera Android
Full support 11.5 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
messageerror event
|
Chrome
Full support 60 |
Edge
Full support 18 |
Firefox
Full support 57 |
IE
? |
Opera
Full support 47 |
Safari
? |
WebView Android
Full support 60 |
Chrome Android
Full support 60 |
Firefox Android
Full support 57 |
Opera Android
Full support 47 |
Safari iOS
? |
Samsung Internet Android
Full support 8.0 |
onmessage
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
No support No |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
onmessageerror
|
Chrome
Full support 60 |
Edge
Full support 18 |
Firefox
Full support 57 |
IE
No support No |
Opera
Full support 47 |
Safari
? |
WebView Android
Full support 60 |
Chrome Android
Full support 60 |
Firefox Android
Full support 57 |
Opera Android
Full support 44 |
Safari iOS
? |
Samsung Internet Android
Full support 8.0 |
postMessage
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
No support No |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
start
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
Full support 10 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
No support No |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
Available in workers | Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support 41 |
IE
Full support Yes |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support 41 |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
See also
MessagePort by Mozilla Contributors is licensed under CC-BY-SA 2.5.