Noodles, Poodles and More!

    cluster-hub
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/cluster-hub package

    0.1.2 • Public • Published

    node-cluster-hub

    A layer for communcation between master and worker processes

    Installation

    npm install cluster-hub
    

    Getting started

    var Hub  = require('cluster-hub');
    var cluster = require('cluster');
    var hub = new Hub();
     
    if (cluster.isMaster) {
        // in master process
        hub.on('sum', function (data, sender, callback) {
            callback(null, data.a + data.b);
        });
     
        var worker = cluster.fork();
    } else {
        //in worker process
        hub.requestMaster('sum', {a: 1, b:2}, function (err, sum) {
            console.log('Sum in worker: ' + sum);
            process.exit();
        });
    }

    Simple message sending

    hub.sendToMaster(message, data);  //works from workers and master
    hub.sendToWorker(worker, message, data);  // works from master
    hub.sendToWorkers(message, data);

    Examples:

    Requests between master and workers (with callback)

    Same as simple messaging, but you can provide a callback

    hub.requestMaster(message, data, callback);  //works from workers and master
    hub.requestWorker(worker, message, data, callback);  // works from master

    Example in "Getting Started" section, and here: https://github.com/sirian/node-cluster-hub/blob/master/examples/requests.js

    Exclusive Locks

    This module provide a way to get global exclusive lock between all processes (master and workers). If worker process dies while holding locked section - lock will be released automatically.

     
    // this method available in master and in workers
    hub.lock(lockKey, function(unlock) {
        // exclusive lock here
       ...
       // to unlock - call unlock()
    })

    Example: https://github.com/sirian/node-cluster-hub/blob/master/examples/locks.js

    Examples

    More examples here: https://github.com/sirian/node-cluster-hub/tree/master/examples

    Install

    npm i cluster-hub

    DownloadsWeekly Downloads

    183

    Version

    0.1.2

    License

    none

    Last publish

    Collaborators

    • sirian