Bird
Raised Fist0
Node.jsframework~20 mins

IPC communication between processes in Node.js - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
IPC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Node.js IPC example?
Consider the following code where a parent process forks a child process and sends a message. What will the parent log after the child responds?
Node.js
const { fork } = require('child_process');

const child = fork('./child.js');

child.on('message', (msg) => {
  console.log('Parent received:', msg);
});

child.send({ greeting: 'hello' });

// child.js
process.on('message', (msg) => {
  process.send({ reply: msg.greeting + ' world' });
});
AParent received: { reply: 'hello world' }
BNo output, process crashes
CParent received: undefined
DParent received: { greeting: 'hello' }
Attempts:
2 left
💡 Hint
Remember that the child sends back a message with a 'reply' property combining the greeting.
📝 Syntax
intermediate
1:30remaining
Which option correctly sets up IPC message listener in a child process?
You want the child process to listen for messages from the parent. Which code snippet is correct?
Aprocess.onMessage((msg) => { console.log(msg); });
Bprocess.on('message', (msg) => { console.log(msg); });
Cprocess.addListener('msg', (msg) => { console.log(msg); });
Dprocess.listen('message', (msg) => { console.log(msg); });
Attempts:
2 left
💡 Hint
Check the exact event name and method to listen for messages on process.
🔧 Debug
advanced
2:30remaining
Why does this IPC message not arrive in the child process?
Given the parent code below, the child never receives the message. What is the cause?
Node.js
const { fork } = require('child_process');
const child = fork('./child.js');

child.send('start');

// child.js
process.on('message', (msg) => {
  console.log('Child got:', msg);
});
AThe child process must be started with execArgv option
BThe message must be an object, not a string
CThe child process file path is incorrect or missing
DThe parent must wait for 'message' event before sending
Attempts:
2 left
💡 Hint
Check if the child process file exists and is correctly referenced.
state_output
advanced
2:00remaining
What is the final value of count after IPC messages?
A parent sends increment commands to a child process which updates a count. What is the child's count after these messages?
Node.js
const { fork } = require('child_process');
const child = fork('./counter.js');

child.send({ cmd: 'inc' });
child.send({ cmd: 'inc' });
child.send({ cmd: 'dec' });

// counter.js
let count = 0;
process.on('message', (msg) => {
  if (msg.cmd === 'inc') count++;
  else if (msg.cmd === 'dec') count--;
});

setTimeout(() => {
  process.send({ count });
}, 100);
A0
B2
C-1
D1
Attempts:
2 left
💡 Hint
Count increments twice then decrements once.
🧠 Conceptual
expert
1:30remaining
Which statement about Node.js IPC channels is true?
Choose the correct statement about IPC communication between parent and child processes in Node.js.
AIPC communication uses a built-in channel that serializes messages as JSON
BIPC channels allow sending only strings between processes
CChild processes can send messages only after the parent sends one first
DIPC messages are synchronous and block the event loop until received
Attempts:
2 left
💡 Hint
Think about how data is transferred between processes and the format used.

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