IPC lets different programs or parts of a program talk to each other. It helps them share data or commands easily.
IPC communication between processes in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
const { fork } = require('child_process');
const child = fork('child.js');
child.on('message', (msg) => {
console.log('Message from child:', msg);
});
child.send({ hello: 'world' });fork() creates a new Node.js process that can talk with the parent.
Use send() to send messages and on('message') to receive them.
Examples
Node.js
const { fork } = require('child_process');
const child = fork('child.js');
child.send('start');Node.js
child.on('message', (msg) => { console.log('Got:', msg); });
Node.js
child.send({ task: 'compute', data: 42 });Sample Program
This example shows a parent process creating a child process. The parent sends a greeting object. The child receives it, prints it, and replies back. The parent then prints the reply.
Node.js
// parent.js
const { fork } = require('child_process');
const child = fork('./child.js');
child.on('message', (msg) => {
console.log('Parent got:', msg);
});
child.send({ greeting: 'Hello Child' });
// child.js
process.on('message', (msg) => {
console.log('Child got:', msg);
process.send({ reply: 'Hello Parent' });
});Important Notes
Messages sent between processes must be serializable (like objects or strings).
IPC is asynchronous, so messages may arrive in any order.
Use IPC to keep your app fast by offloading work to child processes.
Summary
IPC lets Node.js processes talk by sending messages.
Use fork() to create child processes that communicate with the parent.
Send and receive messages with send() and on('message').
Practice
1. What does IPC stand for in Node.js context and why is it useful?
easy
Solution
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.Final Answer:
Inter-Process Communication; it allows processes to exchange messages. -> Option DQuick 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
Solution
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.Final Answer:
child_process.fork() -> Option CQuick 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
Solution
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.Final Answer:
Parent got: Hello World -> Option BQuick 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
Solution
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.Final Answer:
Parent sends message before child process is ready to receive. -> Option AQuick 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
Solution
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.Final Answer:
Parent listens to both children messages, sums numbers, then sends sum to each child using child.send(sum). -> Option AQuick 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
