Node.js process.connected Property
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
const fork = require( 'child_process' ).fork;
const child_file = 'Child.js' ;
const child = fork(child_file);
|
Child.js
console.log( 'In Child.js' )
if (process.connected) {
console.log( "Child.js is connected" );
} else {
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
const fork = require( 'child_process' ).fork;
const child_file = 'Child.js' ;
const child = fork(child_file);
|
Child.js
console.log( 'In Child.js' )
process.disconnect();
if (process.connected) {
console.log( "Child.js is connected" );
} else {
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
Share your thoughts in the comments
Please Login to comment...