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.
;;;; if clusterisMaster for let i=0; i < oslength; i++ cluster; else var data = counter: 0 ; var emitter = ; var app = ; emitter; express; express;
Now let's use cluster-events to do the same job and compare.
;;;; if clusterisMaster for let i=0; i < oslength; i++ cluster; else var data = counter: 0 ; var emitter = "my-emitter"; // provide an unique ID var app = ; emitter; express; express;
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.