BroadcastChannel
What is BroadcastChannel?
Section titled “What is BroadcastChannel?”BroadcastChannel is a browser API that provides a simple publish/subscribe message bus scoped to the same origin. Any tab, iframe, or worker that creates a channel with the same name automatically joins the same group. When one member calls postMessage, every other member receives the message via its onmessage handler.
It is conceptually similar to localStorage’s storage event, but with important differences:
| Feature | BroadcastChannel | storage event |
|---|---|---|
| Message content | Any structured-cloneable value | String only (storage value) |
| Works in workers | Yes (Shared/Service Workers) | No |
| Fires in the sending tab | No | No |
| Requires localStorage write | No — direct | Yes |
Creating and using a channel
Section titled “Creating and using a channel”// Create (or join) a channel by nameconst ch = new BroadcastChannel('my-channel');
// Listen for messagesch.onmessage = (event) => { console.log('Got:', event.data);};
// Send a message to all other membersch.postMessage({ type: 'UPDATE', payload: 42 });
// When done, release resourcesch.close();The name is the only identifier — two pages on the same origin using new BroadcastChannel('chat') are automatically peers. There is no server or registration step.
Event-listener style
Section titled “Event-listener style”You can also use addEventListener instead of the onmessage assignment. This lets you attach multiple listeners:
const ch = new BroadcastChannel('notifications');
ch.addEventListener('message', (e) => { console.log('Handler 1:', e.data);});
ch.addEventListener('message', (e) => { console.log('Handler 2:', e.data);});Closing a channel
Section titled “Closing a channel”Call ch.close() when you no longer need to receive messages. The channel is not shared state — closing in one tab has no effect on other tabs. If you forget to close it, the browser may keep an event-listener reference alive.
const ch = new BroadcastChannel('demo:temp');// ... use it ...ch.close(); // releases internal resourcesRunnable: post and receive on the same channel
Section titled “Runnable: post and receive on the same channel”The demo below creates a channel, attaches a listener, posts a message, and closes after receipt. Because sender and receiver are in the same page the message echoes back — in a real app the listener lives in a separate tab.