How to send a message from the cluster worker process to the master process in Node.js?
Tags: node
Published: 2020-11-01 00:00:00

---
  title: How to send a message from the cluster worker process to the master process in Node.js?
  published: true
  tags: Nodejs
  date: Sun Nov 01 2020 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/sent-message-from-cluster-worker-to-master-process-nodejs
  cover_image: https://melvingeorge.me/_next/static/images/main-d68cdbc4894960cf67c003307f96d2e6.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/sent-message-from-cluster-worker-to-master-process-nodejs)
  
### This blog is related to a previous blog called [How to run the Node.js server on every CPU core?](https://melvingeorge.me/blog/run-nodejs-server-on-every-cpu-core). Go check that out first if you want to learn more about the `Master and Worker process`.

To send a message from a cluster `worker` process to the `master` process, you can use the `send()` method on the `worker` process object.

Let's say we forked a worker process like this,

```js
if (cluster.isMaster) {
  // This block of code is where the master process lives

  // fork a single worker
  const worker = cluster.fork();
} else {
  // This block of code is where worker process lives
}
```

Now let's use the `send()` method to send a message to the master process object (i.e `process` object is used to send messages to the `master` process).

```js
if (cluster.isMaster) {
  // This block of code is where the master process lives

  // fork a single worker
  const worker = cluster.fork();
} else {
  // This block of code is where worker process lives

  // send a mesasge to master process
  // from worker process
  process.send({ data: "Hi Master!" });
}
```

Now to receive the message in the `master` process, we have to listen for any messages from the `worker` process.

This can be done by listening on the `message` event on the `worker` object like this,

```js
if (cluster.isMaster) {
  // This block of code is where the master process lives

  // fork a single worker
  const worker = cluster.fork();

  // listen for any message from any worker process
  worker.on("message", (msg) => {});
} else {
  // This block of code is where worker process lives

  // send a mesasge to master process
  // from worker process
  process.send({ data: "Hi Master!" });
}
```

Inside the `msg` object, you can check for the `data` property where the message is located.

```js
if (cluster.isMaster) {
  // This block of code is where the master process lives

  // fork a single worker
  const worker = cluster.fork();

  // listen for any message from any worker process
  worker.on("message", (msg) => {
    // check for data property
    // on msg object
    if (msg.data) {
      console.log(`Message received to master from worker: ${msg.data}`);
    }
  });
} else {
  // This block of code is where worker process lives

  // send a mesasge to master process
  // from worker process
  process.send({ data: "Hi Master!" });
}
```

We have successfully passed our message from the `worker` process to the `master` process 🔥.

### Feel free to share if you found this useful 😃.

---