All Projects → goldfire → Democracy.js

goldfire / Democracy.js

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Democracy.js

Kiara
Backend-agnostic, lightweight pub/sub library which makes it easy for Go applications to communicate with each other.
Stars: ✭ 129 (-26.29%)
Mutual labels:  pubsub
Wikipedia Ipfs
An exploration to host Wikipedia in IPFS
Stars: ✭ 158 (-9.71%)
Mutual labels:  pubsub
Tigase Server
Highly optimized, extremely modular and very flexible XMPP/Jabber server
Stars: ✭ 170 (-2.86%)
Mutual labels:  pubsub
Rocketman
🚀 Rocketman help build event-based/pub-sub code in Ruby
Stars: ✭ 139 (-20.57%)
Mutual labels:  pubsub
Garagemq
AMQP message broker implemented with golang
Stars: ✭ 153 (-12.57%)
Mutual labels:  pubsub
Rele
Easy to use Google Pub/Sub
Stars: ✭ 164 (-6.29%)
Mutual labels:  pubsub
Event Gateway
React to any event with serverless functions across clouds
Stars: ✭ 1,604 (+816.57%)
Mutual labels:  pubsub
Theyworkforyou
Keeping tabs on the UK's parliaments and assemblies
Stars: ✭ 173 (-1.14%)
Mutual labels:  democracy
Dapr
Dapr is a portable, event-driven, runtime for building distributed applications across cloud and edge.
Stars: ✭ 16,274 (+9199.43%)
Mutual labels:  pubsub
Uwebsockets
Simple, secure & standards compliant web server for the most demanding of applications
Stars: ✭ 13,412 (+7564%)
Mutual labels:  pubsub
Olric
Distributed cache and in-memory key/value data store. It can be used both as an embedded Go library and as a language-independent service.
Stars: ✭ 2,067 (+1081.14%)
Mutual labels:  pubsub
Go Sdk
Dapr SDK for go
Stars: ✭ 149 (-14.86%)
Mutual labels:  pubsub
Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (-5.71%)
Mutual labels:  pubsub
Msgflo
Distributed Flow-Based Programming via message queues
Stars: ✭ 136 (-22.29%)
Mutual labels:  pubsub
Sobjectizer
An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.
Stars: ✭ 172 (-1.71%)
Mutual labels:  pubsub
Syndicate
syn·di·cate: a language for interactive programs
Stars: ✭ 128 (-26.86%)
Mutual labels:  pubsub
Kop
Kafka-on-Pulsar - A protocol handler that brings native Kafka protocol to Apache Pulsar
Stars: ✭ 159 (-9.14%)
Mutual labels:  pubsub
Devforthaifreedom
The hackathon to build technologies for Thailand's journey to democracy and freedom. #devปลดแอก
Stars: ✭ 175 (+0%)
Mutual labels:  democracy
Liftbridge
Lightweight, fault-tolerant message streams.
Stars: ✭ 2,175 (+1142.86%)
Mutual labels:  pubsub
Message Bus
Go simple async message bus
Stars: ✭ 166 (-5.14%)
Mutual labels:  pubsub

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 Democracy = require('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).
});

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.

Events

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].