W3cubDocs

/Web APIs

DedicatedWorkerGlobalScope: postMessage() method

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, options)
postMessage(message, transfer)

Parameters

message

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

options Optional

An optional object containing a transfer field with an array of transferable objects to transfer ownership of. If the ownership of an object is transferred, it becomes unusable in the context it was sent from and it becomes available only to the main thread it was sent to.

transfer Optional

An optional ordered array of transferable objects to transfer ownership of. If the ownership of an object is transferred, it becomes unusable in the context it was sent from and it becomes available only to the main thread it was sent to.

Only transferable objects can be transferred.

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
postMessage 4 12 3.5 10 10.6 4 4.4 18 4 11 5 1.0

See also

The DedicatedWorkerGlobalScope interface it belongs to.

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage