Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"env": {
"es6": true,
"node": true
},
"extends": "airbnb-base",
"rules": {
"object-curly-spacing": ["warn", "never"],
"no-underscore-dangle": 0
}
}
{
"env": {
"es6": true,
"node": true
},
"extends": "airbnb-base",
"rules": {
"object-curly-spacing": ["warn", "never"],
"no-underscore-dangle": 0
}
}
38 changes: 19 additions & 19 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Copyright (c) 2016 James Simpson and GoldFire Studios, Inc.

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
Copyright (c) 2016 James Simpson and GoldFire Studios, Inc.
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.
196 changes: 98 additions & 98 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
## 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](https://www.npmjs.com/package/democracy): `npm install democracy`
* Install with [Yarn](https://yarnpkg.com/en/package/democracy): `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.

```javascript
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
```javascript
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.
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.
## 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](https://www.npmjs.com/package/democracy): `npm install democracy`
* Install with [Yarn](https://yarnpkg.com/en/package/democracy): `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.
```javascript
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
```javascript
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.
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.
68 changes: 34 additions & 34 deletions examples/test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/**
* Basic example usage of democracy.js
*
* Test on your local machine by running three instances of this test script,
* with the parameter being the port. You can then test it by killing the leader
* process to see the re-election happen between the remaining two.
*
* node test.js 12345
* node test.js 12346
* node test.js 12347
*/

var Democracy = require('democracy');

var dem = new Democracy({
source: '0.0.0.0:' + process.argv[2],
peers: ['0.0.0.0:12345', '0.0.0.0:12346', '0.0.0.0:12347']
});

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

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

dem.on('elected', function(data) {
console.log('You are elected leader!');
});

dem.on('leader', function(data) {
console.log('New Leader: ', data);
});
/**
* Basic example usage of democracy.js
*
* Test on your local machine by running three instances of this test script,
* with the parameter being the port. You can then test it by killing the leader
* process to see the re-election happen between the remaining two.
*
* node test.js 12345
* node test.js 12346
* node test.js 12347
*/
var Democracy = require('democracy');
var dem = new Democracy({
source: '0.0.0.0:' + process.argv[2],
peers: ['0.0.0.0:12345', '0.0.0.0:12346', '0.0.0.0:12347']
});
dem.on('added', function(data) {
console.log('Added: ', data);
});
dem.on('removed', function(data) {
console.log('Removed: ', data);
});
dem.on('elected', function(data) {
console.log('You are elected leader!');
});
dem.on('leader', function(data) {
console.log('New Leader: ', data);
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/democracy.js');
module.exports = require('./lib/democracy.js');
Loading