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 MessagePort

Instance methods

Inherits methods from its parent, EventTarget.


Sends a message from the port, and optionally, transfers ownership of objects to other browsing contexts.


Starts the sending of messages queued on the port (only needed when using EventTarget.addEventListener; it is implied when using onmessage).


Disconnects the port, so it is no longer active.


Inherits events from its parent, EventTarget.


Fired when a MessagePort object receives a message.


Fired 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.

const channel = new MessageChannel();
const output = document.querySelector(".output");
const 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!", "*", [

// Handle messages received on port1
function onMessage(e) {
  output.innerHTML =;

For a full working example, see our channel messaging basic demo on GitHub (run it live too).


HTML Standard
# message-ports

Browser compatibility

BCD tables only load in the browser

See also