Giter Site home page Giter Site logo

nathan-schwartz / democracy.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from goldfire/democracy.js

0.0 1.0 0.0 172 KB

Node.js unicast discovery, leader-citizen elections and pub/sub.

License: MIT License

JavaScript 0.21% TypeScript 99.79%

democracy.js's Introduction

Description

In-process monitoring of distributed Node.js instances over UDP unicast. Simply require democracy in each instance and provide the IP/port for other peers and the rest is taken care of automatically. democracy.js will get the configuration from the other nodes, elect a leader and keep them all in sync. If the active leader becomes unresponsive, the other instances will elect a new leader.

Installation

  • Install with npm: npm install democracy
  • Install with Yarn: yarn add democracy

Examples

The below example is easy to run on your local machine (also found in the examples directory). The IP's can just as easily be swapped out for IP's of other servers/instances.

var { default: Democracy } = require('democracy');
// OR import Democracy from 'democracy';

// Basic usage of democracy to manager leader and citizen nodes.
var dem = new Democracy({
  source: '0.0.0.0:12345',
  peers: ['0.0.0.0:12345', '0.0.0.0:12346', '0.0.0.0:12347'],
});

dem.on('added', (data) => {
  console.log('Added: ', data);
});

dem.on('removed', (data) => {
  console.log('Removed: ', data);
});

dem.on('elected', (data) => {
  console.log('You have been elected leader!');
});

// Support for custom events.
dem.on('ciao', (data) => {
  console.log(data.hello); // Logs 'world'
});

dem.send('ciao', {hello: 'world'});

// Support for basic pub/sub.
dem.on('my-channel', (data) => {
  console.log(data.hello); // Logs 'world'
});

dem.subscribe('my-channel');
dem.publish('my-channel', {hello: 'world'});

API

Constructor

new Democracy({
  interval: 1000, // The interval (ms) at which `hello` heartbeats are sent to the other peers.
  timeout: 3000, // How long a peer must go without sending a `hello` to be considered down.
  maxPacketSize: 508, // Maximum size per packet. If the data exceeds this, it will be chunked.
  source: '0.0.0.0:12345', // The IP and port to listen to (usually the local IP).
  peers: [], // The other servers/ports you want to communicate with (can be on the same or different server).
  weight: Math.random() * Date.now(), // The highest weight is used to determine the new leader. Must be unique for each node.
  id: 'uuid', // (optional) This is generated automatically with uuid, but can optionally be set. Must be unique for each node.
  channels: [], // (optional) Array of channels for this node to listen to (for pub/sub).
  enableStrictWeightMode: false, // (optional) Ensure the highest weighted node becomes leader, even if one is already established.
  autoStart: true, // (optional) Whether to start networking upon instantiating or manually at a later time
});

Methods

nodes()

Returns the object containing all active nodes and their properties (including the one the method is called from).

leader()

Returns the current leader node from the cluster.

isLeader()

Returns whether or not the current server is the leader.

resign()

If called on the current leader node, will force it to resign as the leader. A new election will be held, which means the same node could be re-elected.

send(customEvent, data)

Sends a custom event to all other nodes.

subscribe(channel)

Subscribe to a channel for use with pub/sub.

publish(channel, data)

Publish to a channel and send specific data with pub/sub.

start()

Open a socket and join or start a democracy cluster

stop()

Close the socket and leave the democracy cluster

Events

All events return the data/configuration of the affected node as their first parameter.

started

Fired when a socket has been opened and the node has begun communications with the cluster

stopped

Fired when the node has ceased communications with the cluster and closed the socket.

added

Fired when a new peer has been found.

removed

Fired when a peer has gone down and subsequently been removed from the list.

leader

Fired when a new leader is selected.

elected

Fired on the server that has become the new leader.

resigned

Fired on the server that has resigned as the leader.

custom/all other events

Fired on all the server except the one that "sent" the event.

License

Copyright (c) 2016 - 2018 James Simpson and GoldFire Studios, Inc.

Released under the MIT License.

democracy.js's People

Contributors

goldfire avatar raffaeleguidi avatar marcbachmann avatar nathan-schwartz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.