npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

leader

v1.0.1

Published

zookeeper based election

Downloads

12

Readme

node-leader

#Zookeeper based elections

Node-leader is an distributed election library built on top of zookeeper. It is a straight implementation of the Zookeeper Leader-Election algorithm in node.

Internals

You can think of this election as a daisy chain of nodes.

a->b->c->d->e...

Each node will only be aware of the node directly adjacent to it, e.g. b is only aware of a and c. The head of the daisy chain is special and is known as the global leader.

The election is built on top of zk emphemeral sequence nodes under a specific path. Each voter creates a node under the election path (the prefix a,b,c,d,e is optional).

/election/a-00
/election/b-01
/election/c-02
/election/d-03
/election/e-04

As new voters join, they will create new nodes with a monotonically increasing sequence number. When voters expire, their ephemeral nodes are automatically removed by zookeeper.

API

The library emits 4 events as part of its API:

topology This will emit a sorted array of nodes in the election: [a,b,c,d,e] This is the only event that's emitted if you are only watching the election.

gleader This will only be emitted once by the global leader of the election, which in this case is a.

follower This is emitted everytime the current follower of self is updated. e.g. b will get a follower event, with c as its follower.

leader This is emitted everytime the current leader of self is updated. e.g. b will get a leader event, with a as the leader.

Usage

You can use node-leader to either watch an election, or participate in an election. Watching an election means you will only get the topology events, but will not actually create any emphemeral nodes of your own. This is useful for clients or services that depend on the election. By participating in the election, via vote(), you will additionally emit the gleader, leader and follower events. Node-leader expects you to pass it a handle to a already connected node-zookeeper-client.

To watch an election:


var leader = require('node-leader');

...
// create a client and connect to ZK.
var zkClient = ...
...

var watcher = leader.createElection({
    zk: zkClient,
    path: '/glorious_election'
});

watcher.on('topology', function (top) {
    console.log('got election topology', top);
});

To participate in an election:


var leader = require('node-leader');

...
// create a client and connect to ZK.
var zkClient = ...
...

voter = leader.createElection({
    zk: zkClient,
    path: '/glorious_election',
    // the optional prefix of the node path. This is usually used to identify
    // the node in the election. Having a prefix means you can fetch data about
    // this node just from the topology event, without having to explicitly get
    // the node itself.
    prePath: 'kim-jung-number-un',
    // optional data to attach to the ephemeral ZK node.
    data: {'platform': 'all the time Juche is great, Juche is great all the time'}
});

// i am the head of the chain
voter.on('gleader', function () {
    console.log('i am now the global election leader');
});

// the guy in front
voter.on('leader', function (myLeader) {
    console.log('my leader is', myLeader);
});

// the guy behind me
voter.on('follower', function (myFollower) {
    console.log('my follower is', myFollower);
});

voter.on('error', function (err) {
    console.error('got error', err);
});

// join the election.
voter.vote();

Contributions

Contributions are welcome. Please make sure npm test runs cleanly.

License

The MIT License (MIT)

Copyright (c) 2014 Yunong J Xiao

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.