Challenge - 5 Problems
IPC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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' });
});Attempts:
2 left
💡 Hint
Remember that the child sends back a message with a 'reply' property combining the greeting.
✗ Incorrect
The parent sends { greeting: 'hello' } to the child. The child listens for 'message', then sends back { reply: 'hello world' }. The parent logs this reply.
📝 Syntax
intermediate1: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?
Attempts:
2 left
💡 Hint
Check the exact event name and method to listen for messages on process.
✗ Incorrect
The correct event is 'message' and the method to listen is process.on. Other options use incorrect event names or methods.
🔧 Debug
advanced2: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);
});Attempts:
2 left
💡 Hint
Check if the child process file exists and is correctly referenced.
✗ Incorrect
If the child process file path is wrong, the child never starts properly, so it cannot receive messages.
❓ state_output
advanced2: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);Attempts:
2 left
💡 Hint
Count increments twice then decrements once.
✗ Incorrect
Two increments add 2, one decrement subtracts 1, so final count is 1.
🧠 Conceptual
expert1: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.
Attempts:
2 left
💡 Hint
Think about how data is transferred between processes and the format used.
✗ Incorrect
Node.js IPC serializes messages as JSON objects, allowing structured data transfer asynchronously.