npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cruster

v1.1.0

Published

Wrapper on top of node's cluster module. Used for handling communication between entities in an easy manner.

Downloads

9

Readme

cruster

Node.js wrapper on top of the cluster module used for handling workers intercommunication in an easy manner. Please note that I am the only maintainer but I am willing to help with any issue that may appear. Also, I am open for PRs so if you have any improvement you are welcome to pull request.

Also, before you continue, please note that IPC has its own limitations so please try to make the data sent between workers / master as small as possible and try not to flood it channel too much as the node's process could freeze leaving the application unusable.

Usage

Getting Started

The below code snippet is used to create a cluster environment. Note that the code from within the master is only run once while the workers code is run by how many workers are specified. In this case it defaults to how many cores the machine has.

var cruster = require('cruster');
var opts    = {};

opts.master = function(bridge) {

};

opts.workers = function(bridge) {

};

new cruster(opts).run();

Communication

To communicate between workers and master you may use the bridge object following the below syntax.

worker -> master

var cruster = require('cruster');
var opts    = {};

opts.master = function(bridge) {
    bridge.from('*').on('test', function(data) {
        // This method will be called times how many
        // cores your cpu has because the workers count
        // defaults to the core number
    });
    
    bridge.from(1).on('test2', function(data) {
        // This event will be called only when the
        // test2 message comes from worker with bridge.id == 1
    });
};

opts.workers = function(bridge) {
    bridge.to(0).emit('test', null);
    bridge.to(0).emit('test2', null);
};

new cruster(opts).run();

worker -> worker

The worker to worker communication is being handled by the master process. Messages get through the master process and get rerouted to the worker process.

var cruster = require('cruster');
var opts    = {};

opts.master = function(bridge) {

};

opts.workers = function(bridge) {
    if ( bridge.id === 1)
    {
        // Send data to worker with id 2
        bridge.to(2).emit('test', null);
    }
    
    if ( bridge.id === 2 )
    {
        bridge.from(1).on('test', function(data) {
            // Worker with id 2 got data from worker with id 1
        });
    }
};

new cruster(opts).run();

acknowledgement

You may specify a third parameter inside the emit function call that will be called after the worker(s) has/have acknowledged the request. Note that this will increase the messages sent through IPC greatly.

var cruster = require('cruster');
var opts    = {};

opts.master = function(bridge) {

};

opts.workers = function(bridge) {
    if ( bridge.id === 1)
    {
        // Send data to worker with id 2
        bridge.to(2).emit('test', null, function(replies) {
            console.log(replies.get(2)); // Reply from worker with id 2
        });
    }
    
    if ( bridge.id === 2 )
    {
        bridge.from(1).on('test', function(data) {
            // Worker with id 2 got data from worker with id 1
            data.ack('Response');
        });
    }
};

new cruster(opts).run();

Configuration

For handling more complex configurations file paths to workers can be specified.

var cruster = require('cruster');
var opts    = {
    master: './master.js',
    workers: './worker.js'
};

new cruster(opts).run();

You can also specify the number of workers through the count property from inside the workers configuration:

var cruster = require('cruster');
var opts    = {
    master: './master.js',
    workers: {
        handler: './worker.js',
        count: 4 // Spawns 4 workers using file with path ./worker.js
    }
};

new cruster(opts).run();

Or even separate the workers roles by specifying an workers array:

var cruster = require('cruster');
var opts    = {
    master: function(bridge) {
        // You may also pass functions instead of 
        // file paths for both workers and master
    },
    workers: [ // These will be 3 workers
        {
            handler: './worker.js',
            count: 2
        },
        {
            handler: function(bridge) {
            
            }
        }
    ]
};

new cruster(opts).run();

You may also specify functions to run before and after the master process has initialized using the before and after options:

var cruster = require('cruster');
var opts    = {
    master: {
        handler: './master.js',
        before: function() {
            // before initialization
        },
        after: function() {
           // after initialization 
        }
    },
    workers: {
        handler: './worker.js',
        count: 4 // Spawns 4 workers using file with path ./worker.js
    }
};

new cruster(opts).run();

Debug

It's pretty difficult to debug a multi process application and that's why you can use the debug for getting a view on what events are being bind or the flow of the messages.

var cruster = require('cruster');
var opts    = {
    debug: true,
    master: './master.js',
    workers: './worker.js'
};

new cruster(opts).run();