ServiceWorker: 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 April 2018.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
The postMessage()
method of the ServiceWorker
interface sends a message to the worker. The first parameter is the data to send to the worker. The data may be any JavaScript object which can be handled by the structured clone algorithm.
The service worker can send back information to its clients by using the postMessage()
method. The message will not be sent back to this ServiceWorker
object but to the associated ServiceWorkerContainer
available via navigator.serviceWorker
.
Syntax
postMessage(message)
postMessage(message, transfer)
postMessage(message, options)
Parameters
message
-
The object to deliver to the worker; this will be in the
data
field in the event delivered to themessage
event. This may be any JavaScript object handled by the structured clone algorithm.The
message
parameter is mandatory. If the data to be passed to the worker is unimportant,null
orundefined
must be passed explicitly. 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 should be attached to the message; otherwise they would be moved but not actually accessible on the receiving end.
options
Optional-
An optional object containing the following properties:
transfer
Optional-
Has the same meaning as the
transfer
parameter.
Return value
None (undefined
).
Exceptions
SyntaxError
-
Thrown if the
message
parameter is not provided.
Examples
In this example a ServiceWorker
is created and a message is immediately sent:
navigator.serviceWorker.register("service-worker.js");
navigator.serviceWorker.ready.then((registration) => {
registration.active.postMessage(
"Test message sent immediately after creation",
);
});
In order to receive the message, the service worker, in service-worker.js
has to listen to the message
event on its global scope.
// This must be in `service-worker.js`
addEventListener("message", (event) => {
console.log(`Message received: ${event.data}`);
});
Note that the service worker can send back messages to the main thread using the postMessage()
method. To receive it, the main thread needs to listen for a message
event on the ServiceWorkerContainer
object.
Specifications
Specification |
---|
Service Workers # dom-serviceworker-postmessage |
Service Workers # dom-serviceworker-postmessage-message-options |
Browser compatibility
BCD tables only load in the browser
See also
- The
ServiceWorker
interface it belongs to. - Its counterpart, the
postMessage()
method that a service worker must use to send a message back to the associatedServiceWorkerContainer
.