Skip to content

Commit

Permalink
big improvement overall, bumping minor version
Browse files Browse the repository at this point in the history
  • Loading branch information
marcooliveira committed Feb 19, 2014
1 parent 121bd4b commit cbc47a6
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 222 deletions.
26 changes: 17 additions & 9 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@

{
"predef": [
"console"
"console",
"require",
"requirejs",
"define",
"setImmediate",
"clearImmediate",
"requestAnimationFrame",
"cancelAnimationFrame"
],

"browser": false,
"node": true,
"devel": true,

Expand All @@ -12,15 +20,15 @@
"eqeqeq": true,
"forin": false,
"immed": true,
"latedef": false,
"latedef": true,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": true,
"plusplus": false,
"plusplus": true,
"regexp": true,
"undef": true,
"unused": true,
"unused": "vars",
"quotmark": "single",
"strict": true,
"trailing": true,
Expand All @@ -29,18 +37,18 @@
"boss": false,
"debug": false,
"eqnull": true,
"es5": false,
"es3": true,
"esnext": false,
"evil": false,
"expr": false,
"expr": true,
"funcscope": false,
"globalstrict": false,
"iterator": false,
"lastsemic": false,
"laxbreak": false,
"laxcomma": false,
"loopfunc": true,
"multistr": false,
"loopfunc": false,
"multistr": true,
"onecase": true,
"regexdash": false,
"scripturl": false,
Expand All @@ -51,6 +59,6 @@
"validthis": false,

"nomen": false,
"onevar": false,
"onevar": true,
"white": true
}
177 changes: 167 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

Clustering module based on [ØMQ](http://www.zeromq.org/).


## Introduction

*1* is a sort of magnet module, gluing together all the nodes that you launch
in a network, and providing a channel based pub/sub.


## Getting started

Take a look at `bin/one.js`.
*1* (pronounced One) is a sort of magnet module, gluing together all the nodes that you launch in a network, and providing a simple pub/sub. It allows you to separate several services in the same network by means of


## Installation
Expand Down Expand Up @@ -51,6 +42,172 @@ to install *One* on MacOS X, since it depends on
[mdns](https://npmjs.org/package/mdns) and [zmq](https://npmjs.org/package/zmq).


## Getting started

```
var One = require('1');
var one = new One();
// Let's do something when we receive messages.
one.on('message', function (chan, msg) {
console.info(chan + '>', msg);
});
// Join the cluster.
one.join(function (err, cluster) {
err && throw new Error('Unable to join cluster: ' + err);
// Advertise the service.
one.advertise(function (err, adInfo) {
err && throw new Error('Unable to advertise service: ' + err);
// Subscribe a channel
one.subscribe('some_channel', function (err, chan) {
err && throw new Error('Unable to subscribe channel: ' + err);
// Let's send a message to the channel periodically
setTimeout(function () {
one.publish('some_channel', 'You will be notified of this message');
one.publish('some_channel_you_did_not_subscribe', 'You will not get this message');
}, 500);
});
});
});
```

Here's a more elaborate way of instantiating One, with a few extra options:

```
// You can pass a few options when instantiating One.
// Note that these are all optional, and you can instantiate without any option.
// The example below shows all the default options.
var one = new One({
// Id of the service you will be providing.
service: 'unnamedService',
// Cluster which this node belongs to.
cluster: 'defaultCluster',
// Id of this node. If null, a random id is generated.
id: null,
// Port used for publishing messages. If null, a free random port is used.
port: null,
// Interface in which the node will bind.
address: '0.0.0.0'
});
```

## Reference

### Introduction

This module can be used to easily create auto discoverable services that communicate through means of a distributed pub/sub. Unlike solutions based on Redis or some message queueing software, this module is based on 0MQ, enabling you to create a pub/sub without a single point of failure or bottleneck.

### Advertising service

Upon instantiation of *One*, you can specify the `service` which you are providing. This acts as an immediate identifier in case you create multiple service types that you don't want talking to each other. Only after you start advertising other nodes in the cluster will realise you have joined and listen to you. Until that moment, you are a silent node, which is only capable of listening.

Usage:

```
var one = new One({
service: 'myStorageService'
});
// ...
// Advertising service
one.advertise(function (err, adInfo) {
!err && console.log('Advertising', adInfo);
});
// ...
// Stopping advertisement
one.stopAdvertise(function (err, adInfo) {
!err && console.log('Stopped advertising', adInfo);
});
```

### Clustering

Unlike `service`, clustering allows you to partition multiple nodes of the same service in the same network. Basically, only nodes belonging to the same `cluster` will talk to each other.

Usage:

```
var one = new One({
service: 'myStorageService',
cluster: 'cluster1'
});
// Joining cluster
one.join(function (err, cluster) {
!err && console.log('Joined', cluster);
});
// ...
// Leaving cluster
one.leave(function (err, cluster) {
!err && console.log('Left', cluster);
});
```

### Events

Here's a complete list of the available events that you can listen to:

```
one.on('join', function (cluster) {
console.log('joined cluster:', cluster);
});
one.on('leave', function (cluster) {
console.log('left cluster:', cluster);
});
one.on('advertise_start', function (adInfo) {
console.log('started advertising:', adInfo);
});
one.on('advertise_stop', function (adInfo) {
console.log('stopped advertising:', adInfo);
});
one.on('subscribe', function (channel) {
console.log('subscribed:', channel);
});
one.on('unsubscribe', function (channel) {
console.log('unsubscribed:', channel);
});
one.on('node_up', function (node) {
console.log('node up:', node);
});
one.on('node_down', function (node) {
console.log('node down:', node);
});
one.on('message', function (chan, payload) {
console.log(chan + '>', payload);
});
// Note that the error event is only emitted if you do not specify a callback to
// a method that throws an error.
one.on('error', function (err) {
console.error('ERROR: ', err);
});
```


## License

Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
8 changes: 5 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
TODO
====
# TODO

- prevent from using channels with ':'
- unit tests!!!
- prevent from using channels with ':'
- use promises instead of async.waterfal
- review which information I actually eed to return on events/callbacks
115 changes: 0 additions & 115 deletions bin/one.js

This file was deleted.

Loading

0 comments on commit cbc47a6

Please sign in to comment.