
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
node-cluster-ipc
Advanced tools
A lightweight for Inter-Process Communication (IPC) between master and worker processes using the cluster module
node-cluster-ipc is a lightweight Node.js package that simplifies Inter-Process Communication (IPC) for applications using the cluster module. It facilitates message sending, publishing, and requesting between the primary process and worker processes. It also supports request timeout handling and automatic worker selection.
message and request events.To install node-cluster-ipc, run the following command:
$ npm install --save node-cluster-ipc
Here’s a quick example demonstrating how to use node-cluster-ipc:
const cluster = require('cluster');
const { ClusterIpc } = require('node-cluster-ipc');
const ipc = new ClusterIpc();
if (cluster.isPrimary) {
cluster.fork();
cluster.fork();
ipc.publish('hello-channel', 'Hello, worker!');
ipc.request('compute-channel', 42)
.then(response => {
console.log('[Primary] Worker response:', response);
})
.catch(err => {
console.error('[Primary] Error:', err);
});
ipc.on('message', (channel, data) => {
console.log(`[Primary] Received message on ${channel}:`, data);
});
ipc.on('request', (channel, data, reply) => {
console.log(`[Primary] Received request on ${channel}:`, data);
reply(data * 2);
});
} else {
ipc.on('message', (channel, data) => {
console.log(`[Worker] Received message on ${channel}:`, data);
});
ipc.on('request', (channel, data, reply) => {
console.log(`[Worker] Received request on ${channel}:`, data);
reply(data * 2);
});
}
ClusterIpcFirst, instantiate the ClusterIpc class in your primary and worker processes. The constructor accepts an optional ClusterIpcOptions parameter for customizing the request timeout.
import { ClusterIpc } from 'cluster-ipc';
const ipc = new ClusterIpc({
requestTimeout: 5000 // Optional, in milliseconds
});
You can send a message to a specific worker by providing the channel and data. Optionally, specify the workerId to target a specific worker.
ipc.send('channel-name', { key: 'value' }, workerId);
Only the primary process can call publish. This will send a message to all available workers.
ipc.publish('channel-name', { key: 'value' });
You can make requests to workers with request(). It returns a Promise and handles the timeout automatically.
ipc.request('channel-name', { key: 'value' }).then(response => {
console.log('Response:', response);
}).catch(error => {
console.error('Error:', error);
});
You can listen for messages and requests from workers using the message and request events. In case of a request, you can provide a response using the callback function.
ipc.on('message', (channel, data) => {
console.log(`Received message on ${channel}:`, data);
});
ipc.on('request', (channel, data, reply) => {
console.log(`Received request on ${channel}:`, data);
reply({ responseKey: 'responseValue' });
});
new ClusterIPC([options])Initializes a new ClusterIPC instance and sets up either the primary process or the worker process based on the current process type.
options: Configuration options (optional).
requestTimeout: Timeout for requests in milliseconds (default: 5000).const ipc = new ClusterIPC();
.send(channel, data, [workerId])Sends a message to a worker process.
channel: The channel name for the message.data: The data to send.workerId (optional): If provided, the message will be sent to the specific worker. Otherwise, it will be sent to a worker in round-robin order..publish(channel, data)Publishes a message to all active workers (only available in the primary process).
channel: The channel name for the message.data: The data to publish..request(channel, data, [workerId])Sends a request to a worker process.
channel: The channel name for the request.data: The data to send.workerId (optional): If provided, the request will be sent to the specific worker. Otherwise, it will be sent to a worker in round-robin order.Returns Promise for the response.
.isPrimaryReturns true if the process is the primary, otherwise false.
.isWorkerReturns true if the process is a worker, otherwise false.
.workerReturns a reference to the current worker process (only available in a worker process).
.workersReturns an object containing all active worker processes (only available in the primary process).
'message'Listen for messages received by the current process.
channel: The channel of the received message.data: The data sent by the worker (primary).'request'Listen for requests received by the current process and send a response using reply().
channel: The channel of the received message.data: The data sent by the worker (primary).reply: Callback to send a response.FAQs
A lightweight for Inter-Process Communication (IPC) between master and worker processes using the cluster module
We found that node-cluster-ipc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.