https://github.com/mente/jleader
leader election plugin to choose master tab among all same cross origin tabs
https://github.com/mente/jleader
Last synced: about 1 year ago
JSON representation
leader election plugin to choose master tab among all same cross origin tabs
- Host: GitHub
- URL: https://github.com/mente/jleader
- Owner: mente
- Created: 2013-01-25T23:35:52.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2013-12-11T13:18:57.000Z (over 12 years ago)
- Last Synced: 2025-03-15T18:03:48.732Z (over 1 year ago)
- Language: JavaScript
- Size: 125 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
jleader
=======
leader election plugin to choose master tab among all same cross origin tabs
### Introduction
Common case for leader election is long-polling apps. jleader lets you offload server and client by sending connections only from leader tab.
Usage:
```javascript
//Subscribe to leader updates. Callback will be fired when current tab changes it's state.
leader.subscribe(function(newValue) {
if (newValue) {
alert('This tab became master');
} else {
//Tab can lose it's master state if another tab use forces (explicitly calls electMe())
alert('This tab is not master anymore');
}
});
//Announce this tab as a peer. Check whether master is online. If not - elect this tab as master
leader.announce();
//current state is stored here
if (!leader.isMaster) {
//You can force this tab to become master
leader.electMe()
}
```
### How Does it Work?
Plugin is based on HTML5 localStorage fire state event. [jStorage](https://github.com/andris9/jStorage) is used underneath.
[Shared Worker](http://www.w3.org/TR/workers/#shared-workers-introduction) is the best approach but it's [not yet implemented](http://caniuse.com/#feat=sharedworkers) in major browsers. Code should work in all browsers that are supported by jStorage but not all of them were tested.
Tested browsers:
* Chrome (24+)
* Firefox (18+)
* Safari (6+)
* IE (9+)
__Warning__ jleader is not thread safe as localStorage is not thread safe among different tabs. However everything works just fine. There is a [mutex implementation](http://balpha.de/2012/03/javascript-concurrency-and-locking-the-html5-localstorage/) and it will be added if someone encounters problems with thread safeness.