BroadcastChannel: keep WordPress tabs in sync
Many people work with several tabs of the same site open: the shop in one tab, the cart in another, the WordPress admin in a third. When something changes in one tab, the others show old data until they are reloaded. The BroadcastChannel API sends messages between tabs, windows and iframes of the same origin, without a server and without polling.
Problem: A customer adds a product to the cart in one tab and switches to the tab with the cart page, which still shows the old content. In the admin, an editor logs out in one tab, and the other tabs keep failing on save with “cookie check failed” errors.
Solution: Open a BroadcastChannel with a shared name in each tab, post a small message when the cart or the session changes, and let the other tabs react: reload the cart fragment, or show a login notice. Messages are only sent to other tabs, never to the sender.
The channel
// js/ha-tabs.js
const channel = 'BroadcastChannel' in window ? new BroadcastChannel( 'ha-shop' ) : null;
export function haTellOtherTabs( type, data = {} ) {
channel?.postMessage( { type, ...data, at: Date.now() } );
}
export function haOnOtherTab( type, handler ) {
channel?.addEventListener( 'message', ( event ) => {
if ( event.data?.type === type ) {
handler( event.data );
}
} );
}
Every tab of the same origin (scheme, host and port) that opens a channel with the name ha-shop receives the messages of the others. The data is copied with the structured clone algorithm, so plain objects, arrays, dates and maps work, functions and DOM nodes do not. A tab never receives its own messages, which avoids loops.
Sync the cart
import { haTellOtherTabs, haOnOtherTab } from './ha-tabs.js';
// classic WooCommerce cart: jQuery events after AJAX add/remove
jQuery( document.body ).on( 'added_to_cart removed_from_cart updated_cart_totals', () => {
haTellOtherTabs( 'cart-changed' );
} );
// the other tabs ask WooCommerce for fresh fragments (mini cart, counters)
haOnOtherTab( 'cart-changed', () => {
jQuery( document.body ).trigger( 'wc_fragment_refresh' );
} );
wc_fragment_refresh makes the WooCommerce cart fragments script reload the mini cart and the counter by AJAX. Sending only “the cart changed” instead of the cart content keeps the message small and the data correct: each tab gets the current state from the server. On the cart page itself, a reload (location.reload()) is the simplest reaction when the page is not focused.
Logged out in one tab
import { haTellOtherTabs, haOnOtherTab } from './ha-tabs.js';
document.querySelector( '#wp-admin-bar-logout a' )?.addEventListener( 'click', () => {
haTellOtherTabs( 'logged-out' );
} );
haOnOtherTab( 'logged-out', () => {
document.body.insertAdjacentHTML( 'afterbegin',
'<div class="notice notice-warning"><p>You logged out in another tab. Save your work elsewhere and log in again.</p></div>' );
} );
The message only carries the information that something happened; it is not a security mechanism. The server still checks the session on every request. The text of the notice should come from PHP through wp_localize_script() to be translatable.
Close the channel
window.addEventListener( 'pagehide', () => channel?.close() );
A closed channel stops receiving messages. Browsers clean up channels of closed tabs anyway; closing explicitly matters for long-living single-page tools that create channels dynamically. pagehide is used instead of beforeunload, which can prevent the back/forward cache.
NOTE: BroadcastChannel works in Chrome 54, Firefox 38 and Safari 15.4 (March 2022). Private windows and normal windows do not share channels, and neither do different browser profiles. Older code achieves the same with the storage event on localStorage, which fires in other tabs when a key changes. The events inside one page, between components: CustomEvent and EventTarget.