MessagePort: postMessage() method

Baseline Widely available

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 postMessage() method of the MessagePort interface sends a message from the port, and optionally, transfers ownership of objects to other browsing contexts.

Syntax

js
postMessage(message)
postMessage(message, transfer)
postMessage(message, options)

Parameters

message

The message you want to send through the channel. This can be of any basic data type. Multiple data items can be sent as an array.

Note: Execution contexts that can message each other may not be in the same agent cluster, and therefore cannot share memory. SharedArrayBuffer objects, or buffer views backed by one, cannot be posted across agent clusters. Trying to do so will generate a messageerror event containing a DataCloneError DOMException on the receiving end.

transfer Optional

An optional array of transferable objects to transfer ownership of. The ownership of these objects is given to the destination side and they are no longer usable on the sending side. These transferable objects are not automatically sent; they must either be contained in the message or be accessible to the recipient via other means, such as MessagePort via MessageEvent.ports.

options Optional

An optional object containing the following properties:

transfer Optional

Has the same meaning as the transfer parameter.

Return value

None (undefined).

Examples

In the following code block, you can see a new channel being created using the MessageChannel() constructor. When the IFrame has loaded, we pass MessageChannel.port2 to the IFrame using window.postMessage along with a message. The iframe receives the message, and sends a message back on the MessageChannel using postMessage(). The handleMessage handler then responds to a message being sent back from the iframe using onmessage, putting it into a paragraph — MessageChannel.port1 is listened to, to check when the message arrives.

js
const channel = new MessageChannel();
const para = document.querySelector("p");

const ifr = document.querySelector("iframe");
const otherWindow = ifr.contentWindow;

ifr.addEventListener("load", iframeLoaded);

function iframeLoaded() {
  otherWindow.postMessage("Transferring message port", "*", [channel.port2]);
}

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

// in the iframe…

window.addEventListener("message", (event) => {
  const messagePort = event.ports?.[0];
  messagePort.postMessage("Hello from the iframe!");
});

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

Specifications

Specification
HTML
# dom-messageport-postmessage-dev

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
postMessage
Chrome – Full support
Chrome 2 (Release date: 2009-05-21)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Full support
Opera 10.6 (Release date: 2010-07-01)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Full support
Opera Android 11 (Release date: 2011-03-22)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Partial support
Deno 1.12 – 1.13 (Release date: 2021-07-13)
footnote Partial support
footnote The message parameter does not support cloning Blob values.
footnote The transfer parameter does not accept ArrayBuffer items. Passing an ArrayBuffer results in an error being thrown.
Deno – Full support
Deno 1.14 (Release date: 2021-09-14)
footnote
footnote The message parameter does not support cloning Blob values.
Node.js – Full support
Node.js 11.7 (Release date: 2019-01-18)
footnote Full support
options.includeUserActivation parameter
Non-standard
Chrome – Full support
Chrome 72 (Release date: 2019-01-29)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 72 (Release date: 2019-01-29)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 51 (Release date: 2019-03-21)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 11 (Release date: 2019-12-05)
footnote Full support
WebView Android – Full support
WebView Android 72 (Release date: 2019-01-29)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
Bun – No support
Bun
footnote No support
Deno – No support
Deno
footnote No support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Non-standard. Check cross-browser support before using.
See implementation notes.
Has more compatibility info.

See also