Bird
Raised Fist0
Node.jsframework~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does IPC stand for in Node.js context and why is it useful?
easy
A. Internet Protocol Communication; it handles network requests.
B. Internal Process Control; it manages process memory usage.
C. Input-Process-Cache; it speeds up data processing.
D. Inter-Process Communication; it allows processes to exchange messages.

Solution

  1. Step 1: Understand IPC meaning and its use in Node.js

    IPC means Inter-Process Communication, which is about processes exchanging data. In Node.js, IPC lets parent and child processes send messages to coordinate work.
  2. Final Answer:

    Inter-Process Communication; it allows processes to exchange messages. -> Option D
  3. Quick Check:

    IPC = Inter-Process Communication [OK]
Hint: IPC means processes talking to each other [OK]
Common Mistakes:
  • Confusing IPC with network protocols
  • Thinking IPC manages memory or caching
  • Mixing up IPC with internal process controls
2. Which Node.js method is used to create a child process that supports IPC?
easy
A. child_process.spawn()
B. child_process.exec()
C. child_process.fork()
D. child_process.execFile()

Solution

  1. Step 1: Review child process methods and identify the one enabling IPC

    spawn(), exec(), execFile() create processes but don't enable IPC by default. fork() creates a child process with an IPC channel for message passing.
  2. Final Answer:

    child_process.fork() -> Option C
  3. Quick Check:

    fork() creates IPC-enabled child process [OK]
Hint: Use fork() for IPC between parent and child [OK]
Common Mistakes:
  • Using spawn() or exec() expecting IPC
  • Confusing execFile() with fork()
  • Not knowing fork() creates a special IPC channel
3. What will the following code output?
const { fork } = require('child_process');
const child = fork('child.js');
child.on('message', msg => console.log('Parent got:', msg));
child.send('Hello');

// child.js content:
process.on('message', msg => {
  process.send(msg + ' World');
});
medium
A. Parent got: World
B. Parent got: Hello World
C. No output, error occurs
D. Parent got: Hello

Solution

  1. Step 1: Trace the message flow and parent's message handler

    The parent sends 'Hello' to child; child appends ' World' and sends back. Parent logs 'Parent got:' plus the message received from child.
  2. Final Answer:

    Parent got: Hello World -> Option B
  3. Quick Check:

    Child appends ' World' and parent logs it [OK]
Hint: Child appends ' World' before sending back [OK]
Common Mistakes:
  • Expecting parent to log original 'Hello' only
  • Thinking child does not send a message back
  • Confusing message event direction
4. Identify the error in this IPC code snippet:
const { fork } = require('child_process');
const child = fork('child.js');
child.send('Start');
child.on('message', msg => console.log(msg));

// child.js
process.send('Ready');
process.on('message', msg => console.log('Child got:', msg));
medium
A. Parent sends message before child process is ready to receive.
B. Child process sends message before 'message' event listener is set.
C. Child process calls process.send() before parent listens.
D. No error; code works correctly.

Solution

  1. Step 1: Analyze message timing and understand child readiness

    Parent sends 'Start' immediately after fork; child may not be ready yet. Child sends 'Ready' immediately but parent may miss it if not listening yet.
  2. Final Answer:

    Parent sends message before child process is ready to receive. -> Option A
  3. Quick Check:

    Parent must wait for child's 'Ready' before sending [OK]
Hint: Wait for child's ready message before sending [OK]
Common Mistakes:
  • Assuming child is ready immediately after fork
  • Ignoring asynchronous nature of IPC
  • Thinking process.send() order causes error
5. You want to create a Node.js parent process that forks two child processes. Each child sends a number to the parent, and the parent must send back the sum of both numbers to each child. Which approach correctly implements this IPC communication?
hard
A. Parent listens to both children messages, sums numbers, then sends sum to each child using child.send(sum).
B. Each child sends its number to the other child directly using process.send().
C. Parent forks children but does not set up message listeners; children communicate directly.
D. Children send numbers to parent, but parent sends sum only to the first child.

Solution

  1. Step 1: Understand parent-child communication and message handling

    Children cannot send messages directly to each other; parent mediates IPC. Parent listens to messages from both children, calculates sum, then sends sum back to each child.
  2. Final Answer:

    Parent listens to both children messages, sums numbers, then sends sum to each child using child.send(sum). -> Option A
  3. Quick Check:

    Parent mediates and broadcasts sum to children [OK]
Hint: Parent must mediate messages and send sum to all children [OK]
Common Mistakes:
  • Trying to send messages directly between children
  • Not sending sum to both children
  • Not listening to both children's messages in parent