SyntaxStudy
Sign Up
HTML Broadcast Channel API
HTML Advanced 4 min read

Broadcast Channel API

Broadcast Channel API

The Broadcast Channel API lets multiple browser tabs/windows from the same origin communicate with each other.

Example
// In Tab 1 — send message
const channel = new BroadcastChannel("app-sync");
channel.postMessage({ type: "logout" });

// In Tab 2 — receive message
const channel2 = new BroadcastChannel("app-sync");
channel2.onmessage = function(e) {
  if (e.data.type === "logout") {
    location.href = "/login";
  }
};

// Clean up when done
channel.close();
Pro Tip

Broadcast Channel is perfect for keeping multiple tabs in sync after login/logout.