Node.js – process.connected Property


The process.connected property returns True if an IPC channel is connected and will return False after the process.disconnect() method is called. This happens only when the node process is spawned with an IPC channel (i.e., Child process and Cluster).

Once the process.connected property is false, no messages can be sent over the IPC channel.

Syntax

process.connected

Example 1

Create two files "parent.js" and "child.js" as follows −

parent.js

// process.connected Property Demo Example

// Importing the child_process modules
const fork = require('child_process').fork;

// Attaching the child process file
const child_file = 'util.js';

// Spawning/calling child process
const child = fork(child_file);

child.js

console.log('In Child')

// Check if IPC channel is connected
if (process.connected) {

   // Print response messages
   console.log("Child is connected");
} else {

   // Print messages
   console.log("Child is disconnected");
}

Output

C:\home
ode>> node parent.js In Child Child is connected

Example 2

Let’s take a look at one more example.

parent.js

// process.channel Property Demo Example

// Importing the child_process modules
const fork = require('child_process').fork;

// Attaching the child process file
const child_file = 'util.js';

// Spawning/calling child process
const child = fork(child_file);

util.js

console.log('In Child')

// Disconnect with the IPC channel
process.disconnect();

// Check if IPC channel is connected
if (process.connected) {

   // Print response messages
   console.log("Child is connected");
} else {

   // Print messages
   console.log("Child is disconnected");
}

Output

C:\home
ode>> node parent.js In Child Child is disconnected

Updated on: 29-Oct-2021

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements