Skip to content

Provides leadership election in the browser across tabs using localStorage only.

Notifications You must be signed in to change notification settings

PetCareN/tab-election

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tab Election

Provides leadership election, cross tab messaging, and tab data storage in the browser across tabs using localStorage only. It works in modern browsers with localStorage. It is simplified to make the leadership election algorithm work in this limited environment.

It has been optimized so tabs will resolve leadership very quickly, in about 50ms, avoiding a delay in database or server connections and app startup time. After that, when the existing leader is closed, it will take another 50ms to elect a new leader. The exception is when a tab crashes when it may take several seconds.

Install

npm install --save tab-election

API

import { waitForLeadership } from 'tab-election';

waitForLeadership(() => {
  // establish websocket, database connection, or whatever is needed as the leader
});

If a tab needs to stop being a leader (or waiting to become one) you can call close on the returned elector and allow garbage collection.

import { waitForLeadership } from 'tab-election';

let tab = waitForLeadership('myApp', () => {
  // establish websocket, database connection, or whatever is needed as the leader
});

// ... sometime later, perhaps a tab is stale or goes into another state that doesn't need/want leadership
tab.close();

About

Provides leadership election in the browser across tabs using localStorage only.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%