Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
Note: This feature is available in Web Workers.
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.
MessagePort is a transferable object.
EventTarget MessagePortInherits 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 using onmessage).
close()Disconnects the port, so it is no longer active.
Inherits events from its parent, EventTarget.
messageFired when a MessagePort object receives a message.
messageerrorFired when a MessagePort object receives a message that can't be deserialized.
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 outputs the message to a paragraph.
For a full working example, see our channel messaging basic demo on GitHub (run it live too).
| HTML # message-ports |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Mar 6, 2024 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.