Skip to content
Myr

ipc.md

This file

Path

docs/ipc.md

112 lines · 4.4 KiB · Markdown

Last changed by

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–10why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# 📞 Inter-process communication89## Exchanging messages10

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 11why, one step back
11When the [`ipc`](api.md#optionsipc) option is `true`, the current process and subprocess can exchange messages. This only works if the subprocess is a Node.js file.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 12–14why, one step back
12
13The `ipc` option defaults to `true` when using [`execaNode()`](node.md#run-nodejs-files) or the [`node`](node.md#run-nodejs-files) option.14

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 15why, one step back
15The current process sends messages with [`subprocess.sendMessage(message)`](api.md#subprocesssendmessagemessage) and receives them with [`subprocess.getOneMessage()`](api.md#subprocessgetonemessage). The subprocess uses [`sendMessage(message)`](api.md#sendmessagemessage) and [`getOneMessage()`](api.md#getonemessage) instead.

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 16why, one step back
16

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 17–19why, one step back
17```js
18// parent.js19import {execaNode} from 'execa';

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 20why, one step back
20

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 21–25why, one step back
21const subprocess = execaNode`child.js`;
22console.log(await subprocess.getOneMessage()); // 'Hello from child'23await subprocess.sendMessage('Hello from parent');24```25

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 26why, one step back
26```js

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 27–40why, one step back
27// child.js
28import {sendMessage, getOneMessage} from 'execa';2930await sendMessage('Hello from child');31console.log(await getOneMessage()); // 'Hello from parent'32```3334## Listening to messages3536[`subprocess.getOneMessage()`](api.md#subprocessgetonemessage) and [`getOneMessage()`](api.md#getonemessage) read a single message. To listen to multiple messages in a row, [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) and [`getEachMessage()`](api.md#geteachmessage) should be used instead.3738[`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) waits for the subprocess to end (even when using [`break`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) or [`return`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)). It throws if the subprocess [fails](api.md#result). This means you do not need to `await` the subprocess' [promise](execution.md#result).3940```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 41–44why, one step back
41// parent.js
42import {execaNode} from 'execa';4344const subprocess = execaNode`child.js`;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 45–52why, one step back
45await subprocess.sendMessage(0);
4647// This loop ends when the subprocess exits.48// It throws if the subprocess fails.49for await (const message of subprocess.getEachMessage()) {50 console.log(message); // 1, 3, 5, 7, 951 await subprocess.sendMessage(message + 1);52}

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 53–56why, one step back
53```
5455```js56// child.js

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 57why, one step back
57import {sendMessage, getEachMessage} from 'execa';

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 58why, one step back
58

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 59–67why, one step back
59// The subprocess exits when hitting `break`
60for await (const message of getEachMessage()) {61 if (message === 10) {62 break63 }6465 console.log(message); // 0, 2, 4, 6, 866 await sendMessage(message + 1);67}

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 68–69why, one step back
68```
69

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 70–91why, one step back
70## Retrieve all messages
7172The [`result.ipc`](api.md#resultipc) array contains all the messages sent by the subprocess. In many situations, this is simpler than using [`subprocess.getOneMessage()`](api.md#subprocessgetonemessage) and [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage).7374```js75// main.js76import {execaNode} from 'execa';7778const {ipc} = await execaNode`build.js`;79console.log(ipc[0]); // {kind: 'start', timestamp: date}80console.log(ipc[1]); // {kind: 'stop', timestamp: date}81```8283```js84// build.js85import {sendMessage} from 'execa';8687await sendMessage({kind: 'start', timestamp: new Date()});88await runBuild();89await sendMessage({kind: 'stop', timestamp: new Date()});90```91

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 92–95why, one step back
92## Message type
9394By default, messages are serialized using [`structuredClone()`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). This supports most types including objects, arrays, `Error`, `Date`, `RegExp`, `Map`, `Set`, `bigint`, `Uint8Array`, and circular references. This throws when passing functions, symbols or promises (including inside an object or array).95

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 96why, one step back
96To limit messages to JSON instead, the [`serialization`](api.md#optionsserialization) option can be set to `'json'`.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 97–100why, one step back
97
98```js99const subprocess = execaNode({serialization: 'json'})`child.js`;100```

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 101–106why, one step back
101
102## Debugging103104When the [`verbose`](api.md#optionsverbose) option is `'full'`, the IPC messages sent by the subprocess to the current process are [printed on the console](debugging.md#full-mode).105106Also, when the subprocess [failed](errors.md#subprocess-failure), [`error.ipc`](api.md) contains all the messages sent by the subprocess. Those are also shown at the end of the [error message](errors.md#error-message).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 107–112why, one step back
107
108<hr>109110[**Next**: 🐛 Debugging](debugging.md)\111[**Previous**: ⏳️ Streams](streams.md)\112[**Top**: Table of contents](../readme.md#documentation)