Is there any way for two tabs within a browser to share a Mutex (in JavaScript)?
I am working on a web-app using node.js & socket.io, and I want the various tabs to share a single connection with the server. The so-called 'leader' tab is the only one that maintains the connection, while messages to the rest of them are all relayed via this one. Right now, I'm using a leader election algorithm to choose the leader, but given that it takes a second or two to re-elect a new leader if the current one goes down, I wonder if there is a better method to do the same.
Session storage is tab specific and data are not shared between different tabs. Session storage runs in modern browser. sessionStorage.
One of the traditional ways of communicating across Browser Tabs, Popups, and Iframes is Window. postMessage() method. You can send the message as follows. And the target window can listen to the events, as shown below.
The main features of localStorage are: Shared between all tabs and windows from the same origin. The data does not expire. It remains after the browser restart and even OS reboot.
Yes, mutexes can be required in Javascript when accessing resources that are shared between tabs/windows, like localStorage. Between the time that the localStorage item is 'got' and 'set', another tab could have modified the value.
There is a module which solves this by using a leader-election over the BroadcastChannel-API. See
There are various libraries that either try to solve the problem of locking directly or provide message passing between tabs, which you could use as a building block. Check out:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With