Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil). Junte-se a nós e ajude-nos a fazer o trabalho!
Você também pode ler o artigo em English (US).

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

MessagePort.postMessage
Sends a message from the port, and optionally, transfers ownership of objects to other browsing contexts.
MessagePort.start
Starts the sending of messages queued on the port (only needed when using EventTarget.addEventListener; it is implied when using MessagePort.onmessage.)
MessagePort.close
Disconnects the port, so it is no longer active.

Event handlers

Inherits event handlers from its parent, EventTarget

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

Example

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

Specifications

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 Yes18 Yes Yes11.55.1 ?
Available in workers Yes Yes ?41 Yes Yes Yes
close Yes18 Yes No11.55.1 ?
onmessage Yes18 Yes No11.55.1 ?
onmessageerror6060 ?5747 ? ?
postMessage Yes18 Yes No11.55.1 ?
start Yes18 Yes No11.55.1 ?

See also

Etiquetas do documento e colaboradores

Última atualização por: ollelauribostrom,