Broadcast Channel API
The Broadcast Channel API lets multiple browser tabs/windows from the same origin communicate with each other.
The Broadcast Channel API lets multiple browser tabs/windows from the same origin communicate with each other.
// 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();
Broadcast Channel is perfect for keeping multiple tabs in sync after login/logout.