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

cluster-events

v1.1.5

Published

Node's event emitter for cluster workers.

Downloads

15

Readme

Cluster Events

Node's event emitter for cluster workers.

Unlike traditional NodeJS EventEmitter, this module broadcasts the event to all worker processes forked by cluster or child_process (When forking via child_process, you must provide the same entry path for all the workers).

This module doesn't rely on cluster and the master process, so it's perfect using it while your program runs under PM2 supervision.

When will you need to broadcast an event?

Check this example, with traditional EventEmitter, when open the URL in a browser, you will only get data updated in one process, and the other three will still holds the old data.

import { EventEmitter } from "events";
import * as cluster from "cluster";
import * as os from "os";
import * as express from "express";

if (cluster.isMaster) {
    for (let i=0; i < os.cpus().length; i++) {
        cluster.fork();
    }
} else {
    var data = { counter: 0 };
    var emitter = new EventEmitter();
    var app = express();

    emitter.on("updateCounter", () => {
        data.counter++;
    });

    express.get("/update-counter", (req, res) => {
        // Only one process will emit this event and increase the counter.
        emitter.emit("updateCounter");
        res.send("OK");
    }).get("/get-counter", (req, res) => {
        // If there are 4 CPU cores, you have 3/4 of chance to get `0` and only
        // 1/4 of chance to get 1.
        res.send(data.counter);
    });

    express.listen(80);
}

Now let's use cluster-events to do the same job and compare.

import { EventEmitter } from "cluster-events";
import * as cluster from "cluster";
import * as os from "os";
import * as express from "express";

if (cluster.isMaster) {
    for (let i=0; i < os.cpus().length; i++) {
        cluster.fork();
    }
} else {
    var data = { counter: 0 };
    var emitter = new EventEmitter("my-emitter"); // provide an unique ID
    var app = express();

    emitter.on("updateCounter", () => {
        data.counter++;
    });

    express.get("/update-counter", (req, res) => {
        // All processes will emit this event and increase the counter.
        emitter.emit("updateCounter");
        res.send("OK");
    }).get("/get-counter", (req, res) => {
        // No matter how many CPU cores are there, you will only get `1`.
        res.send(data.counter);
    });

    express.listen(80);
}

The only difference of usage is that EventEmitter of cluster-events requires an unique ID to identify the instance among all potential instances, and all other details are just the same, however your event will be emitted to all the listeners in all worker processes.

Limits

Although this module allows you broadcasting the event, however, because the limitation of IPC communication, when you emit an event with some data, you can only pass the data that can be serialized via JSON.stringify(), any other types of data will be lost while transmission (Also, event names only accept strings in this module).

So it's your decision to use this module or the traditional NodeJS EventEmitter, based on your needs.