enmasseio / distribus

A distributed message bus for node.js and the browser.
Apache License 2.0
14 stars 2 forks source link

distribus

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:

distribus architecture

Install

Install the library via npm:

npm install distribus

Use

Sending messages between peers

// 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!');

Publish and subscribe

// 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

Multiple hosts

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');
    });

API

distribus

The distribus namespace contains the following prototypes:

Host

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:

Peer

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:

Roadmap

Related

Test

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

License

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.