MessagePort

Вы читаете английскую версию этой статьи, так как пока нет перевода на данный язык. Помогите нам перевести эту статью!

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

Event handlers

Inherits event handlers from its parent, EventTarget

onmessage
An EventListener called when MessageEvent of type message is fired on the port—that is, when the port receives a message.
onmessageerror
An EventListener called when a MessageEvent of type MessageError 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 the onmessage property.
messageerror
Fired when a MessagePort object receives a message that can't be deserialized.
Also available via the onmessageerror 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 (run it live too).

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'Message ports' in that specification.
Living Standard

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
MessagePortChrome Full support 4Edge Full support YesFirefox Full support YesIE Full support 10Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android Full support YesOpera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support Yes
closeChrome Full support 4Edge Full support 12Firefox Full support YesIE Full support 10Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android No support NoOpera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support Yes
message eventChrome Full support 4Edge Full support 12Firefox Full support YesIE Full support 10Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android No support NoOpera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android ?
messageerror eventChrome Full support 60Edge Full support 18Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 57Opera Android Full support 47Safari iOS ? Samsung Internet Android ?
onmessageChrome Full support 4Edge Full support 12Firefox Full support YesIE Full support 10Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android No support NoOpera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support Yes
onmessageerrorChrome Full support 60Edge Full support 18Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 57Opera Android Full support 44Safari iOS ? Samsung Internet Android Full support Yes
postMessageChrome Full support 4Edge Full support 12Firefox Full support YesIE Full support 10Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android No support NoOpera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support Yes
startChrome Full support 4Edge Full support 12Firefox Full support YesIE Full support 10Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android No support NoOpera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support Yes
Available in workersChrome Full support YesEdge ? Firefox Full support 41IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 41Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also