BroadcastChannel API - 网络的消息总线

BroadcastChannel API 允许同源脚本向其他浏览上下文发送消息。可以将其视为一条简单的消息总线,它允许在窗口/标签页、iframe、Web Worker 和 Service Worker 之间实现 Pub/Sub 语义。

API 基础知识

Broadcast Channel API 是一个简单的 API,可让您更轻松地在浏览上下文之间进行通信。也就是说,在窗口/标签页、iframe、Web Worker 和 Service Worker 之间进行通信。发布到给定频道的消息会传送给该频道的所有监听器。

BroadcastChannel 构造函数只有一个参数:渠道名称。 频道名称可用于识别频道,并且会存在于各种浏览环境下。

// Connect to the channel named "my_bus".
const channel = new BroadcastChannel('my_bus');

// Send a message on "my_bus".
channel.postMessage('This is a test message.');

// Listen for messages on "my_bus".
channel.onmessage = function(e) {
    console.log('Received', e.data);
};

// Close the channel when you're done.
channel.close();

发送消息

消息可以是字符串或结构化克隆算法支持的任何内容(字符串、对象、数组、Blob、数组缓冲区、映射)。

示例 - 发送 Blob 或 File

channel.postMessage(new Blob(['foo', 'bar'], {type: 'plain/text'}));

频道不会向自己广播。如果您有 onmessage 监听器 与postMessage()指向同一频道,则message事件 不会触发。

与其他方法的区别

现在,您可能想知道这与 WebSocket、SharedWorker、MessageChannel APIwindow.postMessage() 等其他消息传递技术有何关系。Broadcast Channel API 不会取代这些 API。每个选项都有各自的用途。Broadcast Channel API 用于在同一源上的脚本之间轻松进行一对多通信。

广播频道的一些应用场景如下:

  • 检测其他标签页中的用户操作
  • 当用户在其他窗口/标签页中登录账号时收到通知。
  • 指示 worker 执行一些后台工作
  • 在服务完成某项操作时获得通知。
  • 当用户在一个窗口中上传照片时,将该照片传递到其他打开的网页。

示例 - 该网页知道用户何时退出登录,甚至可以从同一网站上其他打开的标签页中退出:

<button id="logout">Logout</button>

<script>
function doLogout() {
    // update the UI login state for this page.
}

const authChannel = new BroadcastChannel('auth');

const button = document.querySelector('#logout');
button.addEventListener('click', e => {
    // A channel won't broadcast to itself so we invoke doLogout()
    // manually on this page.
    doLogout();
    authChannel.postMessage({cmd: 'logout', user: 'Eric Bidelman'});
});

authChannel.onmessage = function(e) {
    if (e.data.cmd === 'logout') {
    doLogout();
    }
};
</script>

再举一个例子,假设您想要指示 Service Worker 在用户更改其“离线存储设置”后缓存的内容。 您可以使用 window.caches 删除其缓存,但 Service Worker 可能会 已包含可执行此操作的实用程序。我们可以使用 Broadcast Channel API 来 重复使用该代码!如果没有 Broadcast Channel API,您必须循环处理 self.clients.matchAll() 的结果,并在每个客户端上调用 postMessage(),以实现从 Service Worker 到其所有客户端的通信(执行此操作的实际代码)。使用广播频道会使此 O(1) 而不是 O(N)

示例 - 指示 Service Worker 移除缓存,并重复使用其内部实用程序方法。

index.html 中

const channel = new BroadcastChannel('app-channel');
channel.onmessage = function(e) {
    if (e.data.action === 'clearcache') {
    console.log('Cache removed:', e.data.removed);
    }
};

const messageChannel = new MessageChannel();

// Send the service worker a message to clear the cache.
// We can't use a BroadcastChannel for this because the
// service worker may need to be woken up. MessageChannels do that.
navigator.serviceWorker.controller.postMessage({
    action: 'clearcache',
    cacheName: 'v1-cache'
}, [messageChannel.port2]);

在 sw.js 中

function nukeCache(cacheName) {
    return caches.delete(cacheName).then(removed => {
    // ...do more stuff (internal) to this service worker...
    return removed;
    });
}

self.onmessage = function(e) {
    const action = e.data.action;
    const cacheName = e.data.cacheName;

    if (action === 'clearcache') {
    nukeCache(cacheName).then(removed => {
        // Send the main page a response via the BroadcastChannel API.
        // We could also use e.ports[0].postMessage(), but the benefit
        // of responding with the BroadcastChannel API is that other
        // subscribers may be listening.
        const channel = new BroadcastChannel('app-channel');
        channel.postMessage({action, removed});
    });
    }
};

postMessage() 的差异

postMessage() 不同,您不再需要维护对 iframe 或 worker 的引用即可与其通信:

// Don't have to save references to window objects.
const popup = window.open('https://another-origin.com', ...);
popup.postMessage('Sup popup!', 'https://another-origin.com');

window.postMessage() 还可让您跨源通信。Broadcast Channel API 与同源相同。由于消息保证来自同一来源,因此无需像以前那样使用 window.postMessage() 进行验证:

// Don't have to validate the origin of a message.
const iframe = document.querySelector('iframe');
iframe.contentWindow.onmessage = function(e) {
    if (e.origin !== 'https://expected-origin.com') {
    return;
    }
    e.source.postMessage('Ack!', e.origin);
};

只需“订阅”并实现安全的双向通信!

与 SharedWorker 的区别

如果您需要向可能有多个窗口/标签页或 worker 发送消息,请使用 BroadcastChannel

对于管理锁、共享状态、在服务器和多个客户端之间同步资源或与远程主机共享 WebSocket 连接等更复杂的用例,共享 Worker 是最合适的解决方案。

与 MessageChannel API 的区别

Channel Messaging APIBroadcastChannel 之间的主要区别在于,后者是一种将消息分派给多个监听器(一对多)的方式。MessageChannel 旨在直接在脚本之间进行一对一通信。这一情况也更为复杂,您需要设置两端各有一个端口的通道。

功能检测和浏览器支持

目前,Chrome 54、Firefox 38 和 Opera 41 支持 Broadcast Channel API。

if ('BroadcastChannel' in self) {
    // BroadcastChannel API supported!
}

至于 polyfill,可以使用以下几种:

我没试过这些,所以你的里程数可能会有所不同。

资源