# clusterhub

> Easily and efficiently sync data in your cluster applications.

Latest version **1.1.0** (published 2018-09-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install clusterhub
pnpm add clusterhub
yarn add clusterhub
bun add clusterhub
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-09-24 |
| First published | 2012-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 1 |
| Unpacked size | 20.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 132 |
| Author | fent |
| Maintainers | fent |
| Keywords | cluster, load balance, database, multi process, sync |

## Links

- npm: https://www.npmjs.com/package/clusterhub
- Repository: https://github.com/fent/clusterhub
- Homepage: https://github.com/fent/clusterhub#readme
- Issues: https://github.com/fent/clusterhub/issues
- npm.io page: https://npm.io/package/clusterhub

## Dependencies (1)

- [eventvat](https://npm.io/package/eventvat.md) ^0.2.1

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-09-24
- 1.0.1 — 2018-09-20
- 1.0.0 — 2018-09-20
- 0.3.1 — 2018-02-26
- 0.3.0 — 2017-10-13
- 0.2.11 — 2017-08-10
- 0.2.10 — 2017-02-26
- 0.2.9 — 2016-12-05
- 0.2.8 — 2016-07-05
- 0.2.7 — 2015-09-26
- 0.2.6 — 2015-01-04
- 0.2.5 — 2014-06-25
- 0.2.4 — 2014-01-29
- 0.2.3 — 2013-08-11
- 0.2.2 — 2013-08-04
- … 5 more at https://npm.io/package/clusterhub/versions

## README

# clusterhub

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

[![Build Status](https://secure.travis-ci.org/fent/clusterhub.svg)](http://travis-ci.org/fent/clusterhub)
[![Dependency Status](https://david-dm.org/fent/clusterhub.svg)](https://david-dm.org/fent/clusterhub)
[![codecov](https://codecov.io/gh/fent/clusterhub/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/clusterhub)


# Usage

```js
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][eventvat].

# Motive

Node.js is a perfect candidate to developing [Date Intensive Real-time Applications](http://video.nextconf.eu/video/1914374/nodejs-digs-dirt-about). 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

* [socket.io-clusterhub](https://github.com/fent/socket.io-clusterhub) - Sync data between multi-process socket.io applications.
* [clusterchat](https://github.com/fent/clusterchat) - A multi-process chat that shows off socket.io-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](http://nodejs.org/docs/latest/api/events.html#events.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.

```js
hub.on('remotehello', () => {
  // Hello from another process.
});

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

All functions from [EventVat][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.

[eventvat]: https://github.com/hij1nx/EventVat

#### worker process

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

#### master process
```js
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](https://mochajs.org)

```bash
npm test
```

---
_Source: https://npm.io/package/clusterhub · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
