Skip to content

Commit

Permalink
Merge pull request #5 from goldfire/pub-sub
Browse files Browse the repository at this point in the history
ES6 & Pub/Sub
  • Loading branch information
goldfire authored Feb 14, 2018
2 parents 34afdaa + e1f5877 commit 1252172
Show file tree
Hide file tree
Showing 7 changed files with 1,921 additions and 368 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"env": {
"es6": true,
"node": true
},
"extends": "airbnb-base",
"rules": {
"object-curly-spacing": ["warn", "never"],
"no-underscore-dangle": 0
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,48 @@
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
```
npm install democracy
```
* 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']
peers: ['0.0.0.0:12345', '0.0.0.0:12346', '0.0.0.0:12347'],
});

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

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

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

dem.on('ciao', function(data) {
console.log('ciao from %s', data.id, data.extra);
// 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
Expand All @@ -45,8 +54,9 @@ new Democracy({
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.
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).
});
```

Expand All @@ -59,8 +69,12 @@ Returns the current leader node from the cluster.
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, extraData)
#### 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.
Expand All @@ -79,6 +93,6 @@ Fired on the server that has resigned as the leader.
Fired on all the server except the one that "sent" the event.

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

Released under the MIT License.
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

0 comments on commit 1252172

Please sign in to comment.