1.1.0 • Published 6 years ago

clusterhub v1.1.0

Weekly downloads
54
License
MIT
Repository
github
Last release
6 years ago

clusterhub

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

Build Status Dependency Status codecov

Usage

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

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

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

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

Features

  • Efficient event emitter system. Clusterhub will not send an event to a process that isn't listening for it. Events from the same process of a listener will be emitted synchronously.
  • In process database. Each hub has its own instance of a redis-like database powered by EventVat.

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 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. But you can use this if you want to create more.

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)

Emit an event only to the current process.

Hub#emitRemote(event, ...args)

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

hub.on('remotehello', () => {
  // 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', () => {
  hub.get('foo', (val) => {
    console.log(val === 'bar'); // true
  });
});

master process

let returnedVal = hub.incr('foo', (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

Tests

Tests are written with mocha

npm test
1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.3.1

6 years ago

0.3.0

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

8 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

11 years ago

0.2.2

11 years ago

0.2.1

12 years ago

0.2.0

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago