DedicatedWorkerGlobalScope: 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 July 2015.

Note: This feature is only available in Dedicated Web Workers.

The postMessage() method of the DedicatedWorkerGlobalScope interface sends a message to the main thread that spawned it.

This accepts a data parameter, which contains data to copy from the worker to the main thread. The data may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.

The method also accepts an optional array of transferable objects to transfer to the main thread; Unlike the data parameter transferred objects are no longer usable in the worker thread. (Where possible, objects are transferred using a high performance zero-copy operation).

The main scope that spawned the worker can send back information to the thread that spawned it using the Worker.postMessage method.

Syntax

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

Parameters

message

The object to deliver to the main thread; this will be in the data field in the event delivered to the message event. This may be any value or JavaScript object handled by the structured clone algorithm, which includes cyclical references.

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

The following code snippet shows worker.js, in which an onmessage handler is used to handle messages from the main script. Inside the handler a calculation is done from which a result message is created; this is then sent back to the main thread using postMessage(workerResult);

js
onmessage = (e) => {
  console.log("Message received from main script");
  const workerResult = `Result: ${e.data[0] * e.data[1]}`;
  console.log("Posting message back to main script");
  postMessage(workerResult);
};

In the main script, onmessage would have to be called on a Worker object, whereas inside the worker script you just need onmessage because the worker is effectively the global scope (DedicatedWorkerGlobalScope).

For a full example, see our Basic dedicated worker example (run dedicated worker).

Note: postMessage() can only send a single object at once. As seen above, if you want to pass multiple values you can send an array.

Specifications

Specification
HTML
# dom-dedicatedworkerglobalscope-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
Deno
postMessage
Chrome – Full support
Chrome 4 (Release date: 2010-01-25)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 3.5 (Release date: 2009-06-30)
footnote Full support
Opera – Full support
Opera 10.6 (Release date: 2010-07-01)
footnote Full support
Safari – Full support
Safari 4 (Release date: 2009-06-08)
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 4 (Release date: 2011-03-29)
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 5 (Release date: 2011-10-12)
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 5 (Release date: 2011-10-12)
footnote Full support
Deno – Partial support
Deno 1 – 1.9 (Release date: 2020-05-13)
footnote Partial support
footnote Data passed in the message parameter is serialized with JSON, not the structured clone algorithm.
footnote The transfer parameter is ignored.
Deno – Partial support
Deno 1.10 – 1.11 (Release date: 2021-05-11)
footnote Partial support
footnote The message parameter does not support cloning SharedArrayBuffer or Blob values.
footnote The transfer parameter is ignored.
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.

Legend

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

Full support
Full support
Partial support
Partial support
See implementation notes.
Has more compatibility info.

See also

The DedicatedWorkerGlobalScope interface it belongs to.