Node.js – process.channel Property


When a node process is spawned with an IPC channel, the process.channel property provides the reference to that IPC channel. If no IPC channel exists, this property is then undefined.

Syntax

process.channel

Example 1

Create two files "channel.js" and "util.js" and copy the following code snippets. After creating the files, use the commands "node channels.js" and "node util.js" to run the codes.

channel.js

// process.channel Property Demo Example

// Importing the process modules
const cp = require('child_process');

// Getting reference to the child
const process = cp.fork(`${__dirname}/util.js`);

// Sending the below message to child
process.send({ msg: 'Welcome to Tutorials Point' });

console.log(process.channel)

util.js

// This child will consume the message through channel
process.on('message', (m) => {
   console.log('CHILD got message:', m);

   process.exit()
});

Output

Pipe {
   buffering: false,
   pendingHandle: null,
   onread: [Function],
   sockets: { got: {}, send: {} } }
CHILD got message: { msg: 'Welcome to Tutorials Point' }

Example 2

Let’s take a look at one more example.

// process.channel Property Demo Example

// Importing the process modules
const process = require('process');

// Checking the process channel
if(process.channel)
   console.log("Process Channel exist")
else
   console.log("Process Channel doesn't exist")

Output

Process Channel doesn't exist

Updated on: 29-Oct-2021

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements