• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 13 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sync data in your cluster applications.

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

More Repositories

1

node-ytdl-core

YouTube video downloader in javascript.
JavaScript
4,507
star
2

randexp.js

Create random strings that match a given regular expression.
JavaScript
1,779
star
3

node-ytdl

Command line youtube video downloader.
JavaScript
1,188
star
4

node-m3u8stream

Concatenates segments from a m3u8/dash-mpd playlist into a consumable stream.
TypeScript
213
star
5

node-feedsub

Subscribes to RSS/Atom/JSON feeds and notifies on new items.
TypeScript
196
star
6

timequeue.js

A queue with custom concurrency and time limit.
TypeScript
191
star
7

node-torrent

Torrent reader, writer, and hash checker for node.
JavaScript
190
star
8

feedme.js

RSS/Atom/JSON feed parser
TypeScript
155
star
9

ret.js

Tokenizes a string that represents a regular expression.
JavaScript
90
star
10

socket.io-clusterhub

socket.io storage powered by clusterhub for multi process applications.
JavaScript
70
star
11

chrome-options

Options page for Chrome extensions
JavaScript
64
star
12

node-muk

Mock object methods and dependencies.
JavaScript
57
star
13

node-miniget

A small http(s) GET library.
TypeScript
52
star
14

node-streamify

Streamify helps you easily provide a streaming interface for your code.
JavaScript
51
star
15

pauseable.js

Create event emitters, intervals, and timeouts that can be paused and resumed.
JavaScript
47
star
16

node-streamspeed

A simple way to keep track of the speed of your node streams.
TypeScript
38
star
17

irc-colors.js

Color and formatting for irc bots made easy. Inspired by colors.js and cli-color.
JavaScript
35
star
18

node-kat

File and stream concatenation the right way.
JavaScript
25
star
19

node-stream-equal

Test that two readable streams are equal to each other.
TypeScript
24
star
20

node-streamin

Provide a better streaming api in your app.
JavaScript
20
star
21

npm-updates

Emits update events from the npm repository.
JavaScript
16
star
22

node-jstream

Continuously reads in JSON and outputs Javascript objects.
JavaScript
16
star
23

muk-prop.js

Mock object methods and properties
JavaScript
12
star
24

hook.io-feedsub

hook.io bindings for RSS/Atom/JSON feeds
JavaScript
11
star
25

node-eventyoshi

Allows several event emitters to be listened and emitted through a single one.
JavaScript
11
star
26

node-torrent-cli

CLI for reading, writing, and hash checking torrents
JavaScript
10
star
27

chrome-veefeed

Follow YouTube and Twitch channels, get notifications, categorize videos
JavaScript
9
star
28

pokecry

guess pokemon through their cries
JavaScript
8
star
29

node-newsemitter

An event emitter that emits only new events.
TypeScript
7
star
30

clusterchat

A simple multi process chat demo using socket.io-clusterhub
JavaScript
7
star
31

ordered-queue.js

Queue with concurrency that starts tasks in order and runs them in parallel.
JavaScript
7
star
32

node-module-boilerplate

My personal boilerplate that I use to create node modules.
JavaScript
6
star
33

vim-relative-indent

Hide leading indent
Vim Script
5
star
34

hook.io-npm

Hook that emits on npm module updates.
JavaScript
5
star
35

jps

A scraper for the jpopsuki tracker.
JavaScript
5
star
36

nickserv

Communicates with the NickServ IRC service
JavaScript
5
star
37

node-streamit

JavaScript
4
star
38

ann

IRC bot made to announce and for convenience.
CoffeeScript
4
star
39

vim-frozen

colorscheme for vim
Vim Script
3
star
40

twi

really basic twitter cli client
JavaScript
3
star
41

node-writestreamp

A writable file stream that creates directories as needed.
JavaScript
3
star
42

gifchat

gifphy bot for hipchat
JavaScript
2
star
43

queue2.js

A unordered queue and ordered queue working together.
JavaScript
2
star
44

pokenum

A way to remember numbers using Pokemon.
JavaScript
1
star
45

node-muk-require

Mock dependencies
JavaScript
1
star
46

nickie

Nickserv irc bot.
JavaScript
1
star
47

node-callme

Hey I just met you, and this is crazy, but here's my callback.
JavaScript
1
star
48

jquery.rubber.js

jQuery plugin that stretches text fields to fit their content.
JavaScript
1
star