Service Workers API
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 Client.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
datafield in the event delivered to the ServiceWorkerGlobalScope.message_event event. This may be any JavaScript object handled by the structured clone algorithm.The
messageparameter is mandatory. If the data to be passed to the worker is unimportant,nullorundefinedmust be passed explicitly.[!NOTE] A service worker is not in the same agent cluster as its client, 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 BroadcastChannel/messageerror_event event containing a
DataCloneErrorDOMException on the receiving end.
-
-
transfer- : 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- : An optional object containing the following properties:
transfer- : Has the same meaning as the
transferparameter.
- : Has the same meaning as the
- : An optional object containing the following properties:
Return value
None (undefined).
Exceptions
- SyntaxError
- : Thrown if the
messageparameter is not provided.
- : Thrown if the
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 ServiceWorkerGlobalScope.message_event 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 Client.postMessage() method. To receive it, the main thread needs to listen for a ServiceWorkerContainer.message_event event on the ServiceWorkerContainer object.
Specifications
Browser compatibility
See also
- The ServiceWorker interface it belongs to.
- Its counterpart, the Client.postMessage() method that a service worker must use to send a message back to the associated ServiceWorkerContainer.
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
