大网数据广告活动
Web 标准与前端开发

ServiceWorker: postMessage() method

· 52CE 我爱测

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.

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 data field in the event delivered to the ServiceWorkerGlobalScope.message_event 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 or undefined must 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 DataCloneError DOMException 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 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 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。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。