Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Storage Cross-Tab Communication in JavaScript

Communicate between browser tabs in JavaScript with storage events, BroadcastChannel, shared state and leader election. Simple explanation and real output.

AdvancedJavaScript LocalStorage and SessionStorageExample 10 of 10

Run this in the browser console or a page script. localStorage and sessionStorage are browser features, so plain Node will not have them. Open the page in two tabs to see messages travel.

storage-cross-tab-communication.js
1// Method 1: Basic cross-tab messaging
2class CrossTabMessenger {
3 constructor(channel) {
4 this.channel = channel;
5 this.listeners = [];
6 this.setupListener();
7 }
8
9 setupListener() {
10 window.addEventListener('storage', (e) => {
11 if (e.key === this.channel && e.newValue) {
12 const message = JSON.parse(e.newValue);
13 this.listeners.forEach(listener => listener(message));
14 }
15 });
16 }
17
18 send(message) {
19 localStorage.setItem(this.channel, JSON.stringify({
20 ...message,
21 timestamp: Date.now(),
22 tabId: this.getTabId()
23 }));
24 // Clear immediately to allow same message again
25 setTimeout(() => localStorage.removeItem(this.channel), 100);
26 }
27
28 onMessage(callback) {
29 this.listeners.push(callback);
30 }
31
32 getTabId() {
33 let tabId = sessionStorage.getItem('tabId');
34 if (!tabId) {
35 tabId = 'tab_' + Date.now() + '_' + Math.random();
36 sessionStorage.setItem('tabId', tabId);
37 }
38 return tabId;
39 }
40}
41
42const messenger = new CrossTabMessenger('app-messages');
43messenger.onMessage((message) => {
44 console.log('Message received:', message);
45});
46
47messenger.send({ type: 'user-updated', data: { name: 'John' } });
48
49// Method 2: Broadcast channel API (modern)
50if ('BroadcastChannel' in window) {
51 const channel = new BroadcastChannel('app-channel');
52
53 channel.postMessage({ type: 'notification', message: 'Hello from tab 1' });
54
55 channel.onmessage = function(e) {
56 console.log('Broadcast received:', e.data);
57 };
58}
59
60// Method 3: Shared state manager
61class SharedState {
62 constructor(key) {
63 this.key = key;
64 this.state = this.loadState();
65 this.listeners = [];
66 this.setupListener();
67 }
68
69 loadState() {
70 const stored = localStorage.getItem(this.key);
71 return stored ? JSON.parse(stored) : {};
72 }
73
74 setupListener() {
75 window.addEventListener('storage', (e) => {
76 if (e.key === this.key) {
77 this.state = JSON.parse(e.newValue);
78 this.notifyListeners();
79 }
80 });
81 }
82
83 setState(updates) {
84 this.state = { ...this.state, ...updates };
85 localStorage.setItem(this.key, JSON.stringify(this.state));
86 this.notifyListeners();
87 }
88
89 getState() {
90 return this.state;
91 }
92
93 subscribe(callback) {
94 this.listeners.push(callback);
95 }
96
97 notifyListeners() {
98 this.listeners.forEach(listener => listener(this.state));
99 }
100}
101
102const sharedState = new SharedState('app-state');
103sharedState.subscribe((state) => {
104 console.log('State updated:', state);
105});
106
107sharedState.setState({ user: 'John', theme: 'dark' });
108
109// Method 4: Leader election
110class TabLeader {
111 constructor() {
112 this.leaderKey = 'tab-leader';
113 this.heartbeatInterval = null;
114 this.checkLeader();
115 }
116
117 checkLeader() {
118 const leader = localStorage.getItem(this.leaderKey);
119 const now = Date.now();
120
121 if (!leader) {
122 this.becomeLeader();
123 } else {
124 const [leaderTabId, timestamp] = leader.split(':');
125 const age = now - parseInt(timestamp);
126
127 // If leader is inactive for 5 seconds, take over
128 if (age > 5000) {
129 this.becomeLeader();
130 }
131 }
132
133 // Check every second
134 setTimeout(() => this.checkLeader(), 1000);
135 }
136
137 becomeLeader() {
138 const tabId = this.getTabId();
139 localStorage.setItem(this.leaderKey, tabId + ':' + Date.now());
140 this.startHeartbeat();
141 console.log('Became leader');
142 }
143
144 startHeartbeat() {
145 this.heartbeatInterval = setInterval(() => {
146 const tabId = this.getTabId();
147 localStorage.setItem(this.leaderKey, tabId + ':' + Date.now());
148 }, 2000);
149 }
150
151 isLeader() {
152 const leader = localStorage.getItem(this.leaderKey);
153 if (!leader) return false;
154 const [leaderTabId] = leader.split(':');
155 return leaderTabId === this.getTabId();
156 }
157
158 getTabId() {
159 let tabId = sessionStorage.getItem('tabId');
160 if (!tabId) {
161 tabId = 'tab_' + Date.now();
162 sessionStorage.setItem('tabId', tabId);
163 }
164 return tabId;
165 }
166}
167
168const tabLeader = new TabLeader();

Sample Output

Became leader

The first tab finds no leader key, so it takes the role.

Message received

Only other tabs print this. The sending tab does not get its own storage event or broadcast.

In this one

  • How storage events can carry messages between tabs
  • How BroadcastChannel works
  • How to keep shared state in sync
  • How leader election with a heartbeat works

How it works

  1. 1

    A messenger is created and a message is sent through localStorage.

  2. 2

    A BroadcastChannel posts a notification.

  3. 3

    SharedState sets user and theme and notifies listeners.

  4. 4

    TabLeader finds no leader and takes over.

  5. 5

    The heartbeat refreshes the leader every 2 seconds.

Edge cases

Testing in one tab

Message received and Broadcast received never print.

Both methods only deliver to other tabs. Open a second tab.

Two tabs become leader together

Both read an empty key at the same time.

Storage has no locks. Use the Web Locks API for strict leadership.

Timers never stop

checkLeader and the heartbeat run forever.

Clear the interval and timeout when the tab no longer needs them.

What's going on

Sometimes several tabs of the same app must agree. If a student logs out in one tab, the others should log out too. Cross-tab communication keeps user state, notifications and real-time updates in sync, and lets tabs coordinate.

Method 1 uses storage events. CrossTabMessenger sends a message by writing JSON to a localStorage key. Other tabs get a storage event and pass the parsed message to their listeners. Storage events fire in other tabs, not in the same tab, so the sender does not hear its own message. The key is removed after 100 ms so the same message can be sent again, since writing an identical value does not fire an event. Each message carries a tabId saved in sessionStorage, so every tab has its own id.

Method 2 uses BroadcastChannel, a modern API built for this job. Any tab that opens a channel with the same name gets messages posted to it. It is easier to use and performs better, since nothing is written to disk. A channel object does not receive its own messages, so here too only other tabs print. Browser support has been good for years, but checking 'BroadcastChannel' in window is still wise.

Method 3 is a shared state manager. SharedState keeps an object in localStorage. setState merges updates, saves them and notifies local listeners. Other tabs update through the storage event.

Method 4 is leader election. Only one tab should do some jobs, such as polling the server. The leader writes its id and a timestamp every 2 seconds. This is a heartbeat. Other tabs check every second. If the heartbeat is older than 5 seconds, the leader is gone and a tab takes over.

Best practices: use BroadcastChannel if available, fall back to storage events, handle errors and clean up listeners.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Does a BroadcastChannel receive its own messages?

Pick an answer to check it.

Question 2

When does another tab take over as leader here?

Pick an answer to check it.

Questions people ask

Which method should I use?

BroadcastChannel for messages. Storage events when the data must also be saved.

Why remove the message key after 100 ms?

Writing the same value twice does not fire a storage event.

What is a heartbeat?

A regular signal that says "I am still alive".