A scalable, distributed message bus for node.js and the browser. One or multiple hosts are connected to each other in a peer-to-peer network. Peers can be connected to any of the hosts in the network, and then send messages to each other by their id.
Distribus scales up to hundreds of hosts and millions of peers.
Distribus can be used to:
Install the library via npm:
npm install distribus
// load the library
var distribus = require('distribus');
// create a host
var host = new distribus.Host();
// create two peers
var peer1 = host.create('peer1');
var peer2 = host.create('peer2');
peer1.on('message', function (from, message) {
console.log('peer1 received a message from ' + from + ': ' + message);
peer1.send(from, 'Thanks for your message');
});
peer2.on('message', function (from, message) {
console.log('peer2 received a message from ' + from + ': ' + message);
});
peer2.send('peer1', 'Hi peer1!');
// load the library
var distribus = require('distribus');
// create a host
var host = new distribus.Host();
// subscribe to a channel
host.subscribe('news', function (message) {
console.log('received message:', message);
});
// publish a message
host.publish('news', 'My first message!');
// all subscribers of the channel (on any of the connected hosts) will receive
// the message
Create two files, host1.js
and host2.js
(see examples/multiple_hosts). Start them both with node.
host1.js
var distribus = require('distribus');
var PORT1 = 3000;
var HOST2_URL = 'ws://localhost:3001';
var host1 = new distribus.Host();
var peer1 = host1.create('peer1');
peer1.on('message', function (from, message) {
console.log('Received a message from ' + from + ': "' + message + '"');
if (message.indexOf('hello') === 0) {
peer1.send(from, 'hi ' + from);
}
});
host1.listen('localhost', PORT1)
.then(function () {
return host1.join(HOST2_URL);
})
.then(function () {
console.log('Connected to host2');
var message = 'hello peer2';
console.log('Sending a message to peer2: "' + message + '"');
peer1.send('peer2', message);
})
.catch(function (err) {
console.log('host2 is not running, please start host2.js as well');
});
host2.js
var distribus = require('../../index');
var PORT2 = 3001;
var HOST1_URL = 'ws://localhost:3000';
var host2 = new distribus.Host();
var peer2 = host2.create('peer2');
peer2.on('message', function (from, message) {
console.log('Received a message from ' + from + ': "' + message + '"');
if (message.indexOf('hello') === 0) {
peer2.send(from, 'hi ' + from);
}
});
host2.listen('localhost', PORT2)
.then(function () {
return host2.join(HOST1_URL);
})
.then(function () {
console.log('Connected to host1');
var message = "hello peer1";
console.log('Sending a message to peer1: "' + message + '"');
peer2.send('peer1', message);
})
.catch(function (err) {
console.log('host1 is not running, please start host1.js as well');
});
The distribus namespace contains the following prototypes:
distribus.Host
distribus.Promise
A Host can be created as
var host = new distribus.Host([options]);
The available options are listed under Host.config([options])
.
A Host has the following methods:
Host.close(): Promise.<Host, Error>
Host.config([options]): Object
reconnectTimeout
reconnectDelay
reconnectDecay
.
The initial delay is 1000 ms (1 second) by default.reconnectDecay
Host.create(id: string) : Peer
Peer
.
Throws an error when a peer with the same id already exists on this host.
Does not check whether this id exists on any remote host (use Host.find(id)
to validate this before creating a peer, or even better, use a uuid to
prevent id collisions).Host.find(id: string): Promise.<string, Error>
Host.get(id: string) : Peer
Peer
by its id. Returns null
if the peer does not exist.Host.join(address: string, port: number): Promise.<Host, Error>
Host.listen(address: string, port: number): Promise.<Host, Error>
Host.publish(channel: string, message: *)
Host.remove(peer: Peer | string)
Host.subscribe(channel: string, callback: function)
callback(message)
.Host.unsubscribe(channel: string, callback: function)
A Peer
can send and receive messages. A Peer can be created by a Host.
var host = new distribus.Host();
host.create('peer1')
.then(function (peer) {
console.log(peer.id + ' created');
})
.catch(function (err) {
console.log(err);
});
A Peer has the following functions:
Peer.on(event, callback)
Listen for an event. Available events:
'message'
. Receive a message. Syntax:
Peer.on('message', function (from : String, message: *) {...})
Peer.send(to: String, message: *) : Promise.<null, Error>
Send a message to an other peer. The message must be valid JSON.
To execute tests for the library, install the project dependencies once:
npm install
Then, the tests can be executed:
npm test
To test code coverage of the tests:
npm run coverage
To see the coverage results, open the generated report in your browser:
./coverage/lcov-report/index.html
Copyright (C) 2014 Jos de Jong wjosdejong@gmail.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.