0
0
Node.jsframework~10 mins

IPC communication between processes in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - IPC communication between processes
Parent process starts
Fork child process
Parent sends message
Child processes message
Child sends reply
Processes communicate back and forth
Processes end communication
Shows how a parent process forks a child, then both send and receive messages to communicate.
Execution Sample
Node.js
const { fork } = require('child_process');
const child = fork('child.js');
child.on('message', msg => console.log('Parent got:', msg));
child.send('Hello Child');
Parent process forks a child and sends a message; listens for child's reply.
Execution Table
StepProcessActionMessage SentMessage ReceivedResult
1ParentFork child processChild process created
2ParentSend message to childHello ChildMessage sent to child
3ChildReceive message from parentHello ChildChild got message
4ChildSend reply to parentHello ParentReply sent to parent
5ParentReceive reply from childHello ParentParent got reply
6Parent & ChildContinue communication or endProcesses communicate or close
7Parent & ChildEnd communicationProcesses exit or disconnect
💡 Communication ends when processes close or disconnect.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
childundefinedChild process objectChild process objectChild process object
messageToChildundefined"Hello Child""Hello Child""Hello Child"
messageToParentundefinedundefined"Hello Parent""Hello Parent"
Key Moments - 3 Insights
Why does the parent need to listen for messages from the child?
Because IPC is two-way; the child can send replies or data back, so the parent must listen to receive them (see execution_table step 5).
What happens if the child sends a message before the parent starts listening?
The message might be missed or cause errors; both sides must set up listeners before sending messages to ensure communication works (refer to execution_table steps 2 and 3).
Can the parent and child send messages at any time?
Yes, as long as both have listeners set up, they can send messages asynchronously back and forth (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what message does the parent send at step 2?
A"Hello Child"
B"Hello Parent"
C"Start"
D"Goodbye"
💡 Hint
Check the 'Message Sent' column at step 2 in the execution_table.
At which step does the child send a reply to the parent?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'Send reply to parent' in the 'Action' column of execution_table.
If the parent never listens for messages, what happens at step 5?
AParent receives the message normally
BChild never sends a reply
CParent misses the child's reply
DChild process crashes
💡 Hint
Refer to key_moments about listening before sending and execution_table step 5.
Concept Snapshot
IPC communication in Node.js:
- Parent forks child with fork()
- Both use .send() to send messages
- Both listen with 'message' event
- Messages pass as objects or strings
- Communication is asynchronous and two-way
- Both must set listeners before sending
Full Transcript
In Node.js, IPC communication happens when a parent process creates a child process using fork(). The parent can send messages to the child using child.send(), and the child listens for these messages with the 'message' event. Similarly, the child can send messages back to the parent, which listens for them. This two-way communication allows processes to exchange data asynchronously. Both sides must set up listeners before sending messages to avoid missing data. Communication continues until processes disconnect or exit.