Open In App

Node.js process.connected Property

Improve
Improve
Like Article
Like
Save
Share
Report

The process.connected property is an inbuilt property of the process module which is used by the child process to check if it is connected to the parent process or not. 

Syntax:

process.connected

Return Value: If the process was spawned from another process then the process.connected property will return true if the two process are connected else it will return false.

 

Example 1: If the process is connected then process.connected will return true.

Parent.js




// Require fork method from child_process 
// to spawn child process
const fork = require('child_process').fork;
  
// Child process file
const child_file = 'Child.js';
  
// Spawn child process
const child = fork(child_file);


Child.js




console.log('In Child.js')
  
// If it is connected
if (process.connected) {
  
    // Print messages
    console.log("Child.js is connected");
} else {
  
    // Print messages
    console.log("Child.js is disconnected");
}


Run the Parent.js file using the following command:

node Parent.js

Output:

In Child.js
Child.js is connected

Example 2: If the process is disconnected then process.connected will return false.

Parent.js




// Require fork method from child_process 
// to spawn child process
const fork = require('child_process').fork;
  
// Child process file
const child_file = 'Child.js';
  
// Spawn child process
const child = fork(child_file);


Child.js




console.log('In Child.js')
  
// Disconnect the process
process.disconnect();
  
// If it is connected
if (process.connected) {
  
    // Print messages
    console.log("Child.js is connected");
} else {
  
    // Print messages
    console.log("Child.js is disconnected");
}


Run the Parent.js file using the following command:

node Parent.js

Output:

In Child.js
Child.js is disconnected

Reference: https://nodejs.org/api/process.html#process_process_connected



Last Updated : 25 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads