<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Wilson's Blog]]></title><description><![CDATA[Wilson's Blog]]></description><link>https://blog.tunmii.xyz</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 15:38:44 GMT</lastBuildDate><atom:link href="https://blog.tunmii.xyz/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Exploring the Broadcast Channel API for Real-Time Browser Tab Communication]]></title><description><![CDATA[A couple of years ago, in 2022, I think, my product manager requested an enhancement to ensure that when users log out of the application in one browser tab, they are automatically logged out of all other open tabs (the same application). Initially, ...]]></description><link>https://blog.tunmii.xyz/exploring-the-broadcast-channel-api-for-real-time-browser-tab-communication</link><guid isPermaLink="true">https://blog.tunmii.xyz/exploring-the-broadcast-channel-api-for-real-time-browser-tab-communication</guid><dc:creator><![CDATA[Wilson Adenuga]]></dc:creator><pubDate>Sun, 11 Aug 2024 13:15:06 GMT</pubDate><content:encoded><![CDATA[<p>A couple of years ago, in 2022, I think, my product manager requested an enhancement to ensure that when users log out of the application in one browser tab, they are automatically logged out of all other open tabs (the same application). Initially, I thought of opening all new tabs and saving the instances of the opened windows in a global state that persisted in local or session storage. This seemed complicated, so I did some more research and found out I could achieve this with the Broadcast Channel API.</p>
<h3 id="heading-what-is-broadcast-channel-api">What is Broadcast Channel API?</h3>
<p>According to MDN docs, the Broadcast Channel API allows basic communication between browsing contexts (that is, windows, tabs, frames, or iframes) and workers on the same origin.</p>
<p>The "same origin" aspect means all participating contexts must share the same protocol, host, and port. For example, if you have multiple tabs open, they can communicate with each other using the Broadcast Channel API. However, a tab open to <code>http://example.com</code> (note the different protocol) or <code>https://another-example.com</code> (note the different host) would not be able to join the same communication channel. This restriction ensures that only scripts from the same site can interact, enhancing security by preventing unauthorized cross-origin data sharing.</p>
<p>Below are some examples to illustrate where the Broadcast Channel API will and will not work:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>URL 1</td><td>URL 2</td><td>Will Work?</td></tr>
</thead>
<tbody>
<tr>
<td><code>https://example.com</code></td><td><code>https://example.com/page1</code></td><td>Yes</td></tr>
<tr>
<td><code>https://example.com</code></td><td><code>https://example.com/page2</code></td><td>Yes</td></tr>
<tr>
<td><code>https://example.com</code></td><td><code>https://example.com/subdirectory/page</code></td><td>Yes</td></tr>
<tr>
<td><code>https://example.com:8080</code></td><td><code>https://example.com:8080/page</code></td><td>Yes</td></tr>
<tr>
<td><code>https://sub.example.com</code></td><td><code>https://sub.example.com/page1</code></td><td>Yes</td></tr>
<tr>
<td><code>https://example.com</code></td><td><code>http://example.com</code></td><td>No</td></tr>
<tr>
<td><code>https://example.com</code></td><td><code>https://another-example.com</code></td><td>No</td></tr>
</tbody>
</table>
</div><p>Now, let's write some code</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { useNavigate } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;

<span class="hljs-keyword">const</span> LogoutComponent = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> navigate = useNavigate();

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Create a new BroadcastChannel</span>
      <span class="hljs-keyword">const</span> channel = <span class="hljs-keyword">new</span> BroadcastChannel(<span class="hljs-string">'logout-channel'</span>);

    <span class="hljs-comment">// Function to handle incoming messages</span>
    channel.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
      <span class="hljs-keyword">if</span> (event.data === <span class="hljs-string">'logout'</span>) {
        <span class="hljs-comment">// Perform logout action here</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Received logout message, logging out...'</span>);
        <span class="hljs-comment">// Example: redirect to login page</span>
        navigate(<span class="hljs-string">'/'</span>);
      }
    };

    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-comment">// Clean up the BroadcastChannel on component unmount</span>
      channel.close();
    };
  }, [navigate]);

  <span class="hljs-comment">// Function to send a logout message to other tabs</span>
  <span class="hljs-keyword">const</span> sendLogoutMessage = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> channel = <span class="hljs-keyword">new</span> BroadcastChannel(<span class="hljs-string">'logout-channel'</span>);
    channel.postMessage(<span class="hljs-string">'logout'</span>);
    channel.close();
  };

  <span class="hljs-comment">// Example usage: Call sendLogoutMessage() when user logs out</span>
  <span class="hljs-keyword">const</span> handleLogout = <span class="hljs-function">() =&gt;</span> {
    sendLogoutMessage();
    <span class="hljs-comment">// Also redirect to login page in current tab</span>
    navigate(<span class="hljs-string">'/'</span>);
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleLogout}</span>&gt;</span>
      Log Out
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> LogoutComponent;
</code></pre>
<p>If I have three tabs open—one showing notifications, one for my profile, and another for my dashboard—and I log out from the profile page, the other two tabs will automatically reflect the logout status when I switch to them without having to refresh.</p>
<h3 id="heading-browser-support">Browser Support</h3>
<p>The Broadcast Channel API is widely supported across modern web and mobile browsers, enabling effective cross-tab communication. Below is a summary of its compatibility with various browsers:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Browser</td><td>Supported Version</td></tr>
</thead>
<tbody>
<tr>
<td>Google Chrome</td><td>54+</td></tr>
<tr>
<td>Mozilla Firefox</td><td>38+</td></tr>
<tr>
<td>Microsoft Edge</td><td>79+</td></tr>
<tr>
<td>Opera</td><td>41+</td></tr>
<tr>
<td>Internet Explorer</td><td>Not Supported</td></tr>
</tbody>
</table>
</div><div class="hn-table">
<table>
<thead>
<tr>
<td>Mobile Browser</td><td>Supported Version</td></tr>
</thead>
<tbody>
<tr>
<td>Chrome Android</td><td>54+</td></tr>
<tr>
<td>Firefox for Android</td><td>38+</td></tr>
<tr>
<td>Opera Android</td><td>41+</td></tr>
<tr>
<td>Safari on IOS</td><td>41+</td></tr>
</tbody>
</table>
</div><h3 id="heading-further-reading">Further Reading</h3>
<p>For further reading, you can check out <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API">MDN docs</a>. You can also check out a small chat application i built using.</p>
<p>You can also explore a demo chat application I built to demonstrate the use of Broadcast Channel API on <a target="_blank" href="https://github.com/oluwatunmiisheii/broadcast-api-chat-app">GitHub</a> or try it out <a target="_blank" href="https://broadcast-api-chat-app.vercel.app/">live</a></p>
]]></content:encoded></item></channel></rss>