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.


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 MessagePort.onmessage.)
Disconnects the port, so it is no longer active.

Event handlers

Inherits event handlers from its parent, EventTarget

An EventListener called when a MessageEvent of type message is fired on the port—that is, when the port receives a message.
An EventListener called when a MessageEvent of type MessageError is fired—that is, when it receives a message that cannot be deserialized.


In the following code block, you can see a new channel being created using the MessageChannel.MessageChannel constructor. When the IFrame has loaded, we transfer MessageChannel.port2 (a MessagePort) to the IFrame using MessagePort.postMessage along with a message. The handleMessage handler then responds to a message being sent back from the IFrame (using MessagePort.onmessage), putting it into a paragraph.

var channel = new MessageChannel();
var para = document.querySelector('p');
var ifr = document.querySelector('iframe');
var otherWindow = ifr.contentWindow;

ifr.addEventListener("load", iframeLoaded, false);
function iframeLoaded() {
  otherWindow.postMessage('Hello from the main page!', '*', [channel.port2]);

channel.port1.onmessage = handleMessage;
function handleMessage(e) {
  para.innerHTML =;

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  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support4 Yes Yes1010.65
Available in workers Yes ?41 Yes Yes Yes
close4 Yes Yes1010.65
onmessage4 Yes Yes1010.65
onmessageerror60 ?57 ?47 ?
postMessage4 Yes Yes1010.65
start4 Yes Yes1010.65
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes4 Yes Yes11.55.1 ?
Available in workers Yes Yes ?41 Yes Yes Yes
close Yes4 Yes No11.55.1 ?
onmessage Yes4 Yes No11.55.1 ?
onmessageerror6060 ?5747 ? ?
postMessage Yes4 Yes No11.55.1 ?
start Yes4 Yes No11.55.1 ?

See also

Document Tags and Contributors

Last updated by: fscholz,