Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Easily and efficiently sync data in your cluster applications.

branch: master

v0.2.4

[ci skip]
latest commit a130491969
Roly Fentanes authored January 29, 2014
Octocat-spinner-32 lib Listen to disconnect event as well as exit January 28, 2014
Octocat-spinner-32 test coding styling August 10, 2013
Octocat-spinner-32 .gitignore first commit February 11, 2012
Octocat-spinner-32 .travis.yml add node v0.10 March 19, 2013
Octocat-spinner-32 LICENSE first commit February 11, 2012
Octocat-spinner-32 README.md instructions for node v0.6 July 05, 2012
Octocat-spinner-32 package.json v0.2.4 January 29, 2014
README.md

clusterhub Build Status

An attempt at giving multi process node programs a simple and efficient way to share data.

Usage

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var hub = require('clusterhub');

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

} else {
  hub.on('event', function(data) {
    // do something with `data`
  });

  // emit event to all workers
  hub.emit('event', { foo: 'bar' });
}

Features

  • Efficient event emitter system. Clusterhub will waste no time sending an event to a process that isn't listening for it. Events from the same process of a listener will be immediately emitted.
  • In process database. Each hub has its own instance of a redis-like database powered by EventVat.
  • Cluster agnostic. Apps that use clusterhub will work regardless if it uses cluster or not.

Motive

Node.js is a perfect candidate to developing Date Intensive Real-time Applications. Load balancing in these applications can become complicated when having to share data between processes.

A remote database can be an easy solution for this, but it's not the most optimal. Communicating with a local process is several times faster than opening remote requests from a database. And even if the database is hosted locally, the overhead of communicating with yet another program is lessened.

Note that this module is still experimental. It currently works by using a process's internal messaging system.

Made with Clusterhub

API

hub.createHub(id)

Clusterhub already comes with a default global hub. Use this if you want to create a custom hub.

Hub#destroy()

Call to disable hub from emitting and receiving remote messages/commands.

Additionally, all functions from the regular EventEmitter are included. Plus a couple of extras.

Hub#emitLocal(event, [args...])

Use this to emit an event only to the current process.

Hub#emitRemote(event, [args...])

Use this to emit an event only to other worker processes and master. Or only to workers if the current process is the master.

hub.on('remotehello', function() {
  // hello from another process
});

hub.emitRemote('remotehello', { hello: 'there' });

All functions from EventVat are included as well. Their returned value can be accessed by providing a callback as the last argument. Or optionally by its returned value if called by the master.

worker process

hub.set('foo', 'bar', function() {
  hub.get('foo', function(val) {
    console.log(val === 'bar'); // true
  });
});

master process

var returnedVal = hub.incr('foo', function(val) {
  // can be given a callback for consistency
  console.log(val === 1); // true
});

// but since it's the master process it has direct access to the database
console.log(returnedVal === 1); // true

Install

npm install clusterhub

To use with node v0.6.x look at the v0.1.x tag.

npm install clusterhub@0.1.x

Tests

Tests are written with mocha

npm test

License

MIT

Something went wrong with that request. Please try again.