Suggest an editImprove this articleRefine the answer for “What is IPC (inter-process communication)?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**IPC (Inter-Process Communication)** is a mechanism that lets isolated processes exchange data; in Node.js it's built into `child_process`/`fork()` and `cluster`, implemented via Unix Domain Sockets (Linux/macOS) or Named Pipes (Windows). **Key point:** IPC cannot pass functions or context references - only serializable data (objects, strings, buffers).Shown above the full answer for quick recall.Answer (EN)Image## What IPC is > **IPC (Inter-Process Communication)** is a mechanism > that lets **processes exchange data** with each other. In other words: > if you have two (or more) processes, > **IPC** gives them a "channel" so they can **send and receive messages** > (as if they were "talking" over a wire). ## 1. Why IPC is needed Every process: - runs **in its own address space** (its own memory, its own environment), - is **isolated** from others (for security reasons). So they **can't directly share variables** or memory. To interact, they need a **communication channel**. **IPC** solves this by letting processes: - pass data (text, JSON, buffers), - coordinate work (signals, events), - synchronize state. ## 2. Types of IPC (in general, not just in Node.js) | IPC type | Description | Example | |---|---|---| | **Pipe** | Sequential data transfer (one writes, one reads) | stdin/stdout | | **Message Passing** | Passing messages (objects) through system sockets or channels | Node.js `process.send()` | | **Shared Memory** | Shared memory for fast exchange | `SharedArrayBuffer` | | **Sockets** | Universal network exchange between processes | TCP/UDP | | **Signals** | Simple OS signals (`SIGINT`, `SIGTERM`) | `process.kill(pid, 'SIGINT')` | | **Files / Named Pipes** | Exchange via temp files / named channels | UNIX socket, FIFO | ## 3. IPC in Node.js In Node.js, **IPC is built into**: - `child_process` - `cluster` When you create a process via: ```javascript const { fork } = require('child_process'); ``` Node.js **automatically creates an IPC channel** between parent and child. That lets you: - send messages (`child.send(data)`); - receive them (`process.on('message', handler)`). ## 4. An IPC example in Node.js **main.js** ```javascript const { fork } = require('child_process'); // Create a child process const child = fork('worker.js'); // Send a message child.send({ task: 'compute', number: 5 }); // Get the reply child.on('message', (msg) => { console.log('Reply from the child process:', msg); }); ``` **worker.js** ```javascript process.on('message', (msg) => { if (msg.task === 'compute') { const result = msg.number ** 2; process.send({ result }); } }); ``` What happens: - `fork()` creates an IPC channel between parent and child. - The parent sends the object `{ task, number }`. - The child receives it via `process.on('message')`. - After processing, it sends the result back via `process.send()`. That's **Inter-Process Communication** in action. ## 5. IPC in the `cluster` module The `cluster` module also uses IPC under the hood: - **master** ↔ **worker** talk through the same mechanism. - Messages can be passed manually: ```javascript // master worker.send({ msg: 'ping' }); // worker process.on('message', (data) => { console.log('Message from the master:', data); process.send({ reply: 'pong' }); }); ``` So `cluster` uses IPC to exchange both internal events and user data. ## 6. How IPC works under the hood in Node.js Node.js implements IPC through **Unix Domain Sockets** (on Linux/macOS) or **Named Pipes** (on Windows). In other words: > a **virtual socket** is created between processes, > and messages are serialized and sent over it. These messages: - are serialized (for example, as JSON or a binary format), - travel through an internal channel (a pipe), - are deserialized on the receiving side. ## 7. IPC and data serialization Through IPC, you can pass: - objects (`{name: 'Tim'}`), - strings, numbers, buffers, - even **references to sockets or file descriptors**. Node.js automatically serializes objects on transfer: ```javascript child.send({ user: 'Alice', age: 25 }); ``` But keep in mind: - **functions** or **context references** cannot be passed; - data is passed by value, not by reference. ## 8. Advantages of IPC | Advantage | Description | |---|---| | Two-way communication | Parent and child processes can exchange data | | Security | Each process is isolated, only the channel connects them | | Performance | IPC on one machine is faster than HTTP | | Universality | Works between any Node.js processes (`fork`, `cluster`) | | Fault tolerance | An error in one process doesn't break the others | ## 9. Drawbacks of IPC | Drawback | Description | |---|---| | Serialization/deserialization | Adds latency when passing large objects | | Memory isolation | Data can't be shared directly, only via copies | | Synchronization complexity | With many processes, queues need managing | | Not for large data streams | Sockets or streams are a better fit | ## Summary | Criterion | Description | |---|---| | Purpose | Communication between processes | | Implementation in Node.js | Via `fork()` and `cluster` | | Direction | Two-way (parent ↔ child) | | Data type | JSON, strings, buffers | | Under the hood | Unix Socket / Named Pipe | | Advantages | Safe, isolated, efficient | | Drawbacks | Can't share memory directly | **Conclusion:** > **IPC (Inter-Process Communication)** is the mechanism > that lets processes in Node.js (or an OS in general) > **talk and pass data to each other** without sharing memory. > > In Node.js, IPC is used in the `child_process` and `cluster` modules, > where processes exchange messages through a built-in communication channel.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.