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 = ;var opts = {}; opts { }; opts { }; opts;
Communication
To communicate between workers and master you may use the bridge object following the below syntax.
worker -> master
var cruster = ;var opts = {}; opts { bridge; bridge;}; opts { bridge; bridge;}; opts;
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 = ;var opts = {}; opts { }; opts { if bridgeid === 1 // Send data to worker with id 2 bridge; if bridgeid === 2 bridge; }; opts;
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 = ;var opts = {}; opts { }; opts { if bridgeid === 1 // Send data to worker with id 2 bridge; if bridgeid === 2 bridge; }; opts;
Configuration
For handling more complex configurations file paths to workers can be specified.
var cruster = ;var opts = master: './master.js' workers: './worker.js'; opts;
You can also specify the number of workers through the count property from inside the workers configuration:
var cruster = ;var opts = master: './master.js' workers: handler: './worker.js' count: 4 // Spawns 4 workers using file with path ./worker.js ; opts;
Or even separate the workers roles by specifying an workers array:
var cruster = ;var opts = { // 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 { } ; opts;
You may also specify functions to run before and after the master process has initialized using the before and after options:
var cruster = ;var opts = master: handler: './master.js' { // before initialization } { // after initialization } workers: handler: './worker.js' count: 4 // Spawns 4 workers using file with path ./worker.js ; opts;
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 = ;var opts = debug: true master: './master.js' workers: './worker.js'; opts;