Mutex Lock (JS) Divided between multiple browser tabs?

Is there a way so that two tabs in the browser can share Mutex (in JavaScript)?

I am working on a web application using node.js and socket.io, and I want the different tabs to be able to share the same connection to the server. The so-called tab "leader" is the only one that supports the connection, while messages for the rest of them are transmitted through this one. Right now, I'm using a leader selection algorithm to select a leader, but given that re-selecting a new leader requires a second or second, if the current one goes down, I wonder if there is a better way to do the same.

+9
source share
3 answers

There are various libraries that either try to solve the lock problem directly, or provide message passing between tabs, which can be used as a building block. Departure:

+1
source

Each tab is independent, and therefore browsers probably do not allow JS on one tab to influence or change another. The only exception to the above is when one tab spawns another using JavaScript.

However, you can explore the Chrome or Firefox Addon extension . Both of these features provide the features you are looking for.

Alternative workflow:

You can have one main tab that opens the rest using window.open () . The function returns a link to an open window, which can then be used in combination with window.postMessage () .

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]); //windowObjectReference is a reference to the newly created window. 
0
source

There is also another new library that does this in a fairly reliable way!

  • works in all browsers
  • provides locking both within one tab and between tabs
  • if you take a lock on a tab and the tab closes before it is released, then the next time someone tries to get a lock, it will ultimately be successful.
  • It has the usual semantics of locking, as in any other programming language.

https://www.npmjs.com/package/browser-tabs-lock

0
source

All Articles