# BroadcastChannel

> The `BroadcastChannel` JS object allows communication between browsing contexts of the same origin.

## How do I send messages between browser tabs in JavaScript?

The object that sends messages between tabs, windows, iframes and workers of the same origin through a named channel. Often used to sync logout or state changes across open tabs.

- Topic: Specials
- Type: object
- Browser support: Chrome 54+, Chrome Android 54+, Edge 79+, Firefox 38+, Firefox Android 38+, Safari 15.4+, Safari iOS 15.4+

## Example

Wrong:

```js
window.addEventListener('storage', (e) => {
  if (e.key === 'logout') signOut();
});
localStorage.setItem('logout', Date.now());
```

Right:

```js
const channel = new BroadcastChannel('auth');
channel.onmessage = (e) => {
  if (e.data.type === 'logout') signOut();
};
channel.postMessage({ type: 'logout' });
```

`BroadcastChannel` sends structured messages to same-origin tabs directly, without abusing storage writes.

## Common mistakes

- Trying to talk to a page on another origin, which never receives the message.

## Key points

- The channel is closed with `close()` when the page or component no longer needs it.
- The code does not expect the sender to receive its own message.
- Messages contain plain data that can be cloned, not functions or DOM nodes.

## Interview questions

### Does BroadcastChannel deliver a message to the sender?

No, the sending channel object does not get its own message, but other `BroadcastChannel` objects with the same name do, even in the same tab.

### Can BroadcastChannel talk across different origins?

No, it only connects windows, tabs, iframes and workers of the same origin. Use `window.postMessage()` for cross-origin messages.

## Related cards

- [localStorage](https://wonderium.dev/js/local-storage.md)
- [Worker](https://wonderium.dev/js/worker.md)
- [structuredClone()](https://wonderium.dev/js/structured-clone.md)
- [navigator.locks](https://wonderium.dev/js/navigator-locks.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/broadcast-channel
- Last updated: 2026-11-25
