Broadcast Channels

Amresh Kumar
4 min readApr 23, 2023

--

Broadcast channels are a powerful feature of modern web browsers that allow different JavaScript code snippets running in separate windows or tabs of the same browser to communicate with each other. This feature opens up a whole new world of possibilities for web developers, enabling them to build sophisticated real-time applications with minimal effort. In this article, we will explore what broadcast channels are, how they work, and some practical use cases for this feature.

What are Broadcast Channels?

Broadcast channels are a mechanism for sending messages between different windows or tabs of the same web browser, regardless of the origin or domain of the windows. In other words, different web pages or web applications running in separate browser tabs or windows can communicate with each other using broadcast channels.

Broadcast channels are based on the MessageChannel API, which provides a low-level interface for creating channels for sending and receiving messages. The API defines two types of objects: a MessageChannel object and a MessagePort object. A MessageChannel object represents a communication channel between two endpoints, while a MessagePort object represents an endpoint of a MessageChannel.

How do Broadcast Channels Work?

When you create a new MessageChannel object, it creates two MessagePort objects, one for each endpoint of the channel. You can then send messages between these endpoints using the postMessage() method of the MessagePort object. The postMessage() method takes a single argument, which is the message to send. You can send any JavaScript object as a message, as long as it can be serialized into JSON.

To receive messages on the other end of the channel, you need to add an event listener to the MessagePort object using the onmessage property. When a message is received, the onmessage event is triggered, and you can access the message using the event.data property.

Practical Use Cases for Broadcast Channels

Broadcast channels can be used in a wide range of real-time applications, such as online games, collaborative editors, chat applications, and more. Here are some practical use cases for broadcast channels:

  1. Real-time Collaboration: Broadcast channels can be used to enable real-time collaboration between different users of a web application. For example, you can use broadcast channels to synchronize the state of a collaborative editor or to update the status of an online game in real-time.
  2. Cross-Tab Communication: Broadcast channels can be used to enable communication between different tabs of the same web application. For example, you can use broadcast channels to synchronize data between different tabs of a web application, or to update the user interface of a web application in real-time.
  3. Cross-Origin Communication: Broadcast channels can be used to enable communication between different web pages or web applications running on different domains or origins. For example, you can use broadcast channels to integrate third-party widgets or to enable cross-domain messaging between different web applications.

Let’s say you have a web application that displays real-time stock prices for different companies. Each time a new stock price is received from the server, the application needs to update the UI in all open tabs of the application.

To accomplish this, you can create a new broadcast channel using the MessageChannel API, and then use this channel to send the new stock price to all open tabs of the application. Here is how the code might look:

// Create a new broadcast channel
const channel = new BroadcastChannel('stock-prices');

// Listen for new stock prices on the channel
channel.addEventListener('message', (event) => {
const stockPrice = event.data;
// Update the UI with the new stock price
});

// Send a new stock price to all open tabs
function sendStockPrice(stockPrice) {
channel.postMessage(stockPrice);
}

In this example, we create a new broadcast channel with the name ‘stock-prices’. We then listen for new messages on the channel using the addEventListener() method, and update the UI with the new stock price whenever a message is received.

To send a new stock price to all open tabs of the application, we simply call the sendStockPrice() function, passing in the new stock price as a parameter. This function uses the postMessage() method of the broadcast channel to send the new stock price to all open tabs of the application.

By using broadcast channels, we can synchronize the UI of our web application across different tabs in real-time, without having to rely on complex server-side synchronization or polling mechanisms.

Conclusion

Broadcast channels are a powerful feature of modern web browsers that enable different JavaScript code snippets running in separate windows or tabs of the same browser to communicate with each other. This feature opens up a whole new world of possibilities for web developers, enabling them to build sophisticated real-time applications with minimal effort. If you are building a real-time application or a collaborative web application, consider using broadcast channels to simplify your implementation and improve the user experience.

Congratulations on completing this article, which is part of my series, Top Web APIs Every Frontend Developer Should Know. If you’re eager to discover more about other essential APIs, make sure to check out the rest of the series. Each article dives deep into a different set of APIs, providing valuable insights and practical examples to help you level up your frontend development skills.

If you found this article helpful, don’t forget to clap for it! 👏 It helps me know what content you enjoy and want to see more of.

Also, feel free to connect with me on LinkedIn to stay updated with my latest articles and insights into software development. Let’s grow and learn together!

--

--

Amresh Kumar

Software Engineer with 3 years of experience in building full-stack application.