0.1.2 • Public • Published

tab-elect Build Status npm

Leader election for browser tabs

Sauce Test Status

Tab-elect solves the problem of only wanting one browser tab to run a job, and ensure that there is always one browser tab running it even if the previous running tab was closed. This package takes care of the inter-tab communication required to elect one tab as the leader and ensure that there is always a leader even if the previous leader quits. A browser tab gets notified when it has been elected as the new leader, and this allows the tab to run whatever code the leader should run. When another tab takes the leadership position, the previous leader gets notified that it has been deposed.

Tab-elect uses the atomic operations of IndexedDB to ensure that only one leader gets elected even if many candidates attempt to elect themselves simultaneously. BroadcastChannels are used to broadcast when a new leader has taken over; because of this there is a short amount of time during the transition from one leader to another that both instances think they are the leader.


var te = TabElect('campain name')
console.log('Am I the leader?', te.isLeader)
te.on('elected', function () {
  console.log('I am the leader!')
te.on('deposed', function () {
  console.log('I am no longer the leader')
te.on('error', function (err) {
  console.log('Error', err)


var te = new TabElect(name)

Constructs a new TabElect instance with the given name. name is used to group instances together so instances with different names will have different elections.


A boolean that indicates if this instance is the leader.


Attempt to elect this instance as the leader. cb is called with cb(err, elected) where err is null if there were no errors and elected is true if the instance is now the leader. This method throws an exception if it is called while the instance is the leader.


Forces the instance to relinquish it's leadership status. All other tab-elect instances will attempt to become the next leader. If the instance is not the leader then this method throws an exception.


Destroys the instance and frees all internal resources. If this instance was the leader than a new leader will be elected. No more events will be emitted.


te.on('elected', function () {})

The instance is the new leader

te.on('deposed', function () {})

The instance is no longer the leader

te.on('error', function () {})

A critical error occurred. This instance attempts to relinquish it's leader status and destroys itself.


MIT. Copyright (c) Austin Middleton.

Package Sidebar


npm i tab-elect

Weekly Downloads






Last publish


  • xuset