Socket
Book a DemoInstallSign in
Socket

voter

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

voter

Distributed voting based on Scuttlebutt

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

voter

Distributed voting based on Scuttlebutt

Installation

npm install voter

Usage

var Voter = require('voter');

var timeout = 100;

function onNew(key, voting) {
  console.log(this.id + ': voting ' + key + ' with quorum ' + voting.quorum + ' started');
  setTimeout(function () {
    this.vote(key, true);
  }.bind(this), timeout += 100);
}

function onVote(key, origin, vote) {
  console.log(this.id + ': ' + origin + ' voted: ' + JSON.stringify(vote));
}

function onQuorum(key, vote) {
  console.log(this.id + ': voting ' + key + ' reached quorum: ' + JSON.stringify(vote));
}

var a = new Voter();
var b = new Voter();
var c = new Voter();

a.on('new', onNew);
b.on('new', onNew);
c.on('new', onNew);

a.on('vote', onVote);
b.on('vote', onVote);
c.on('vote', onVote);

a.on('quorum', onQuorum);
b.on('quorum', onQuorum);
c.on('quorum', onQuorum);

var ab = a.createStream();
var bc = b.createStream();

ab.pipe(b.createStream()).pipe(ab);
bc.pipe(c.createStream()).pipe(bc);

a.startVoting('voting', { quorum: 4, timeout: 10000 });

setTimeout(function () {
  var d = new Voter();
  var cd = c.createStream();
  cd.pipe(d.createStream()).pipe(cd);

  d.on('new', onNew);
  d.on('vote', onVote);
  d.on('quorum', onQuorum);

}, 4000);

Keywords

p2p

FAQs

Package last updated on 13 Nov 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts