# 广播频道 API

**广播频道 API**（Broadcast Channel API）是一种 Web 平台 API，它允许同源且位于同一存储分区内的浏览上下文中的脚本——包括窗口、标签页、框架、iframe 和 worker——通过构造具有相同频道名称的 `BroadcastChannel` 对象，在同一设备上相互发送一对多消息。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup> HTML 标准将其描述为一种基于频道的广播机制，适用于共享 worker 会造成不合理开销的简单场景，例如通知其他标签页“用户在这里登录了”。<sup>[2](https://html.spec.whatwg.org/multipage/web-messaging.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 作用范围 | 同一设备上同一存储分区内、同源浏览上下文与 worker 之间的一对多消息传递；发送方 `BroadcastChannel` 对象不会收到自己的消息<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup><sup> • </sup><sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup> |
| 序列化 | 消息使用结构化克隆算法进行序列化；函数、DOM 节点和 Symbol 会抛出 `DataCloneError`<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup><sup> • </sup><sup>[5](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)</sup> |
| 投递 | 每个符合条件的已连接频道（发送方除外）上都会触发 `message` 事件；没有持久化，因此新打开的标签页会错过其订阅之前发布的消息<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/postMessage)</sup><sup> • </sup><sup>[5](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)</sup> |
| 分区 | 通信仅限于同一存储分区内，按顶层站点进行分区<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup> |
| 浏览器支持 | Firefox 38（2015 年）、Chrome 54（2016 年）、Safari 15.4（2022 年）；自 2024-09-14 起达到基线广泛可用<sup>[6](https://web-platform-dx.github.io/web-features-explorer/features/broadcast-channel/)</sup> |
| 使用率 | 约在 16.048% 的 Chrome 页面加载中使用<sup>[6](https://web-platform-dx.github.io/web-features-explorer/features/broadcast-channel/)</sup> |
| 典型用途 | 跨标签页传播登录/登出和会话过期；向 service worker 发送缓存失效信号<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup><sup> • </sup><sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup> |

## 目的与背景

有时，共享同一源的文档——由同一用户在同一用户代理中打开，但位于不同且互不相关的浏览上下文中——必须相互通知。HTML 标准的动机示例是凭据刷新：一个标签页告诉其他标签页用户已登录，它们应重新检查自己的凭据。<sup>[2](https://html.spec.whatwg.org/multipage/web-messaging.html)</sup> Chrome 团队将该 API 定位为同源上简便的一对多通信方式，它不取代 [WebSocket](https://www.edgepedia.org/zh/websockets)、SharedWorker、MessageChannel 或 `window.postMessage`。<sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup>

相比共享 worker 的优势在于搭建成本：任何上下文都可以按名称加入频道，无需持有对其他框架或 worker 的引用，也无需运行专用的 worker 脚本。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup>

## 工作原理

**构造与绑定。** 上下文使用频道名称创建一个 `BroadcastChannel`。在同一源和存储分区中，所有使用相同名称构造的对象构成一个频道，进行双向通信。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup> 在内部，每个对象携带其频道名称和一个关闭标志，投递仅限于存储键和频道名称与发送方匹配的频道。<sup>[2](https://html.spec.whatwg.org/multipage/web-messaging.html)</sup>

**发送与接收。** `postMessage()` 接受任何可序列化的对象，并将其发送给任何具有相同源和存储分区的浏览上下文中的每个监听器；每个接收方 `BroadcastChannel` 都会触发 `message` 事件。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/postMessage)</sup> 数据使用结构化克隆算法进行序列化，因此包括字符串、普通对象、数组、`Blob`、`ArrayBuffer` 和 `Map` 在内的多种对象都可以发送，无需手动序列化。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup><sup> • </sup><sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup> 如果频道已经关闭，`postMessage()` 会抛出 `InvalidStateError` `DOMException`；如果数据的任何部分不可序列化，则抛出 `DataCloneError`。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/postMessage)</sup> `SharedArrayBuffer` 对象或由其支持的视图无法跨代理集群传递；尝试这样做会在接收端生成一个包含 `DataCloneError` 的 `messageerror` 事件。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/postMessage)</sup>

**自我排除与资格。** 发送方 `BroadcastChannel` 对象不会收到自己的消息；同一页面中具有相同名称的另一个 `BroadcastChannel` 对象不会因此被排除。<sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup> 规范将目标限制为有资格进行消息传递的上下文——具有完全活动文档的 `Window`，或未处于关闭或可挂起状态的 worker 全局作用域——并将发送方从目标列表中移除。<sup>[2](https://html.spec.whatwg.org/multipage/web-messaging.html)</sup>

**关闭。** 调用 `close()` 会将对象与频道断开连接，并允许其被垃圾回收。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup> 规范强烈建议在不再需要时显式关闭 `BroadcastChannel` 对象，因为注册了监听器的对象会被强引用保持存活，可能表现为内存泄漏。<sup>[2](https://html.spec.whatwg.org/multipage/web-messaging.html)</sup>

**无定义协议。** 消息传递协议本身不由规范定义；已连接的上下文必须实现自己的消息格式和处理方式，不进行协商，规范也不提出任何要求。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup>

## 浏览器支持与兼容性

Firefox 于 2015-05-12 在版本 38 中发布 BroadcastChannel，Chrome 于 2016-10-12 在版本 54 中发布，Safari 于 2022-03-14 在版本 15.4 中发布；Edge 79 和 Safari iOS 15.4 也支持它，而 Safari 3.1 至 15.3 不支持。<sup>[6](https://web-platform-dx.github.io/web-features-explorer/features/broadcast-channel/)</sup> 该功能于 2024-09-14 达到基线广泛可用。<sup>[6](https://web-platform-dx.github.io/web-features-explorer/features/broadcast-channel/)</sup>

对于较旧的浏览器，基于 `localStorage`、[IndexedDB](https://www.edgepedia.org/zh/indexeddb) 或套接字构建的 shim 实现可以复现频道接口。<sup>[7](https://caniuse.com/broadcastchannel)</sup> Bugcrowd 的生产实现分层使用三种传输方式：在可用时使用原生 API，对于没有原生支持但支持 WebWorker 的浏览器使用 IndexedDB，对于不支持 WebWorker 的较旧浏览器使用 `localStorage`。<sup>[8](https://bugcrowd.engineering/blogs/session-expiry-on-broadcast-channel)</sup>

## 数据一览

Chrome 平台状态报告显示，BroadcastChannel 约在 16.048% 的页面加载中使用。<sup>[6](https://web-platform-dx.github.io/web-features-explorer/features/broadcast-channel/)</sup>

序列化和反序列化是主要的性能开销，它们会阻塞各自的 realm：序列化阻塞发送方 realm，反序列化阻塞接收方 realm。Chrome 和 Safari 将反序列化推迟到访问 `.data` 时，而 Firefox 在派发之前进行反序列化。实测每条消息的开销在桌面版 Chrome 上约为 5 微秒，在 Chrome Android 上约为 100 微秒，在桌面版 Firefox 和桌面版 Safari 上约为 1 毫秒；传输时间与载荷的 JSON 字符串长度相关，在超过约 100 KiB 后变得显著。<sup>[9](https://surma.dev/things/is-postmessage-slow/)</sup> 这些基准测试涵盖的是一般性的结构化克隆消息传递，而非专门针对 BroadcastChannel。

BroadcastChannel 没有记录在案的固定消息大小限制。2011 年一项关于跨上下文通信的学术调查对替代频道的容量限制进行了分类——Cookie 为 4 KB，`window.name` 为 2 MB，`localStorage` 为 5 MB——并将 postMessage 风格的频道消息传递视为没有最大消息长度，尽管它指出，出于安全或可靠性考虑（例如避免内存耗尽），实现可能会对此类本无上限的系统的消息大小施加上限。<sup>[10](https://ar5iv.labs.arxiv.org/html/1108.4770)</sup>

## 顺序与投递保证

来自单个发送方的消息按顺序到达单个接收方，但当多个发送方同时发布时，接收方可能以不同的顺序看到消息；需要因果顺序的应用必须在载荷中放入序列号或时间戳。<sup>[5](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)</sup>

BroadcastChannel 不是持久化存储，也没有记忆：订阅时没有当前值可读，订阅者只接收其开始监听之后发布的消息，因此新打开的标签页会错过之前发送的任何内容。<sup>[5](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)</sup> 结合发送方的自我排除，这意味着在没有其他监听器的情况下发布的消息根本不会被投递到任何地方。<sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup>

## 与其他消息传递机制的对比

- **`window.postMessage`** 可跨源工作，要求接收方验证发送方的源；BroadcastChannel 是同源的，因此接收到的消息无需进行源验证。<sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup> 源锁定是精确的：协议、主机和端口必须匹配，因此同一主机的 `https` 和 `http` 版本属于不同的源。<sup>[5](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)</sup>
- **SharedWorker** 仍然是复杂场景的合适解决方案，例如管理共享状态的锁、在服务器和多个本地客户端之间同步资源，或与远程主机共享单个 `WebSocket` 连接。<sup>[2](https://html.spec.whatwg.org/multipage/web-messaging.html)</sup>
- **Service Worker 消息传递**需要枚举客户端：通过 BroadcastChannel 广播缓存失效指令使通信变为 O(1)，而不是通过 `self.clients.matchAll()` 循环并对每个客户端调用 `postMessage` 的 O(N) 操作。<sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup> 任何浏览器上下文都可以通过 `BroadcastChannel` 对象的 `onmessage` 方法监听，作为与 service worker 之间的双向频道，无需获取 worker 或任何特定客户端的显式引用。<sup>[11](https://web.dev/articles/two-way-communication-guide)</sup>
- **`localStorage` 事件**在 shim 中作为常见的回退传输方式，代价是 5 MB 的存储上限和仅限字符串的载荷。<sup>[7](https://caniuse.com/broadcastchannel)</sup><sup> • </sup><sup>[10](https://ar5iv.labs.arxiv.org/html/1108.4770)</sup>

## 安全、隐私与存储分区

按照最初的规范，BroadcastChannel 允许具有不同顶层域的同源内容相互通信，这使得这些内容中的脚本可以跨不同顶层站点跟踪用户。这促使了一项分区提案，该提案在 WebKit 中实现（bug 229814 和 230164），并通过 whatwg/html PR 7567 纳入 HTML 标准，分区存储键在存储标准中定义。<sup>[12](https://github.com/whatwg/html/issues/5803)</sup>

在分区模型下，存储首先按顶层站点进行分区：如果 `a.com` 上的页面嵌入了一个来自 `b.com` 的 iframe，而另一个页面直接打开 `b.com`，则该 iframe 无法与第二个页面通信，尽管两者在技术上同源。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup> 分区破坏了真实的集成：一款企业产品报告称，其 BroadcastChannel 在一个知名 CRM 应用中 iframe 内的 Web 应用实例与另一个窗口中的实例之间停止工作，Chrome 的缓解途径是进行源试用以禁用第三方存储分区。<sup>[12](https://github.com/whatwg/html/issues/5803)</sup>

按源划分的作用范围在单个站点内也有实际后果：不同子域上的标签页被视为不同的源，因此每个子域需要自己的协调标签页。<sup>[8](https://bugcrowd.engineering/blogs/session-expiry-on-broadcast-channel)</sup>

## 实际用途

最常见的生产用途是身份验证同步：检测同一源上其他标签页中的用户操作，例如用户登录或登出时，并传播会话过期，使每个标签页一起做出反应。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)</sup><sup> • </sup><sup>[5](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)</sup> Bugcrowd 的实现使用每个源一个领导者标签页来轮询会话状态并广播过期，并配有上述 IndexedDB 和 `localStorage` 回退方案。<sup>[8](https://bugcrowd.engineering/blogs/session-expiry-on-broadcast-channel)</sup>

第二类用途是协调缓存。将新数据写入本地缓存的标签页可以通知过时的标签页重新获取，页面可以通过频道指示 service worker 删除缓存内容，将 O(N) 的客户端循环变为单次广播。<sup>[5](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)</sup><sup> • </sup><sup>[3](https://developer.chrome.com/blog/broadcastchannel)</sup>

## References

1. [Broadcast Channel API, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API)
2. [HTML Standard, Web messaging (BroadcastChannel), WHATWG](https://html.spec.whatwg.org/multipage/web-messaging.html)
3. [BroadcastChannel API: A message bus for the web, Chrome for Developers](https://developer.chrome.com/blog/broadcastchannel)
4. [BroadcastChannel: postMessage() method, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/postMessage)
5. [BroadcastChannel for Cross-Boundary Communication, Steve Kinney](https://stevekinney.com/courses/enterprise-ui/broadcast-channel)
6. [BroadcastChannel, Web features explorer (web-features)](https://web-platform-dx.github.io/web-features-explorer/features/broadcast-channel/)
7. [BroadcastChannel, Can I use](https://caniuse.com/broadcastchannel)
8. [Session Expiry using Browser Broadcast-channel, Bugcrowd Engineering](https://bugcrowd.engineering/blogs/session-expiry-on-broadcast-channel)
9. [Is postMessage slow?, surma.dev](https://surma.dev/things/is-postmessage-slow/)
10. [A Classification Framework for Web Browser Cross-Context Communication, arXiv:1108.4770](https://ar5iv.labs.arxiv.org/html/1108.4770)
11. [Two-way communication with service workers, web.dev](https://web.dev/articles/two-way-communication-guide)
12. [BroadcastChannel needs partitioning, WHATWG HTML issue 5803](https://github.com/whatwg/html/issues/5803)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web开发与Web平台技术 › Web平台API

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/broadcast-channel-api

English original: https://www.edgepedia.org/broadcast-channel-api
