# The BroadcastChannel API

> Learn the basics of 1-to-many communication with the BroadcastChannel API: create a channel, send data with postMessage(), and listen for the message event.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2019-05-23 | Topics: [Web Platform](https://flaviocopes.com/tags/platform/) | Canonical: https://flaviocopes.com/broadcastchannel-api/

The [Channel Messaging API](https://flaviocopes.com/channel-messaging-api/) is a great way to send 1-to-1 messages from a window to an iframe, from a window to a Web Worker, and so on.

The BroadcastChannel API can be used to send 1-to-many messages, communicating to multiple entities at the same time.

You start by initializing a `BroadcastChannel` object:

```js
const channel = new BroadcastChannel('thechannel')
```

To send a message on the channel you use the `postMessage()` method:

```js
channel.postMessage('Hey!')
```

A message can be any of those supported values:

- All primitive types, excluding symbols
- [Arrays](https://flaviocopes.com/javascript-array/)
- Object literals
- [String](https://flaviocopes.com/javascript-string/), [Date](https://flaviocopes.com/javascript-dates/), [RegExp](https://flaviocopes.com/javascript-regular-expressions/) objects
- [Blob](https://flaviocopes.com/blob/), [`File`](https://flaviocopes.com/file/), [`FileList`](https://flaviocopes.com/filelist/) objects
- [`ArrayBuffer`](https://flaviocopes.com/arraybuffer/), [`ArrayBufferView`](https://flaviocopes.com/arraybufferview/) objects
- [FormData](https://flaviocopes.com/formdata/) objects
- ImageData objects
- [Map](https://flaviocopes.com/javascript-data-structures-map/) and [Set](https://flaviocopes.com/javascript-data-structures-set/) objects

To receive messages from the channel, listen to the `message` event:

```js
channel.onmessage = (event) => {
  console.log('Received', event.data)
}
```

This event is fired for all listeners, except the one that is sending the message.

You can close the channel using:

```js
channel.close()
```
