Skip to content
Myr

ipc.md

This file

Path

docs/ipc.md

174 lines · 5.8 KiB · Markdown

Last changed by

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

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](node.md).

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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#subprocessgetonemessagegetonemessageoptions).

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 16why, one step back
16

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 17why, one step back
17The subprocess uses [`sendMessage(message)`](api.md#sendmessagemessage) and [`getOneMessage()`](api.md#getonemessagegetonemessageoptions). Those are the same methods, but imported directly from the `'execa'` module.

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 18why, one step back
18

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 19–21why, one step back
19```js
20// parent.js21import {execaNode} from 'execa';

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 22why, one step back
22

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 23why, one step back
23const subprocess = execaNode`child.js`;

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 24–25why, one step back
24await subprocess.sendMessage('Hello from parent');
25const message = await subprocess.getOneMessage();

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 26why, one step back
26console.log(message); // 'Hello from child'

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 27–28why, one step back
27```
28

Add user guides (#989)ehmickybefore adoptionbe91ecf

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 30why, one step back
30// child.js

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 31why, one step back
31import {getOneMessage, sendMessage} from 'execa';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 32why, one step back
32

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

lines 33–35why, one step back
33const message = await getOneMessage(); // 'Hello from parent'
34const newMessage = message.replace('parent', 'child'); // 'Hello from child'35await sendMessage(newMessage);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 36–39why, one step back
36```
3738## Listening to messages39

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 40why, one step back
40The methods described above read a single message. On the other hand, [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage) and [`getEachMessage()`](api.md#geteachmessage) return an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). This should be preferred when listening to multiple messages.

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 41–44why, one step back
41
42[`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).4344```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 45–48why, one step back
45// parent.js
46import {execaNode} from 'execa';4748const subprocess = execaNode`child.js`;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 57–60why, one step back
57```
5859```js60// child.js

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 62why, one step back
62

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 63–65why, one step back
63// The subprocess exits when hitting `break`
64for await (const message of getEachMessage()) {65 if (message === 10) {

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 66why, one step back
66 break;

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 67–71why, one step back
67 }
6869 console.log(message); // 0, 2, 4, 6, 870 await sendMessage(message + 1);71}

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 72–73why, one step back
72```
73

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 74–93why, one step back
74## Filter messages
7576```js77import {getOneMessage} from 'execa';7879const startMessage = await getOneMessage({80 filter: message => message.type === 'start',81});82```8384```js85import {getEachMessage} from 'execa';8687for await (const message of getEachMessage()) {88 if (message.type === 'start') {89 // ...90 }91}92```93

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

lines 94–95why, one step back
94## Retrieve all messages
95

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 96why, one step back
96The [`result.ipcOutput`](api.md#resultipcoutput) array contains all the messages sent by the subprocess. In many situations, this is simpler than using [`subprocess.getOneMessage()`](api.md#subprocessgetonemessagegetonemessageoptions) and [`subprocess.getEachMessage()`](api.md#subprocessgeteachmessage).

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

lines 97–101why, one step back
97
98```js99// main.js100import {execaNode} from 'execa';101

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

lines 102–104why, one step back
102const {ipcOutput} = await execaNode`build.js`;
103console.log(ipcOutput[0]); // {kind: 'start', timestamp: date}104console.log(ipcOutput[1]); // {kind: 'stop', timestamp: date}

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

lines 105–113why, one step back
105```
106107```js108// build.js109import {sendMessage} from 'execa';110111await sendMessage({kind: 'start', timestamp: new Date()});112await runBuild();113await sendMessage({kind: 'stop', timestamp: new Date()});

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 114–135why, one step back
114```
115116## Send an initial message117118The [`ipcInput`](api.md#optionsipcinput) option sends a message to the [Node.js subprocess](node.md) when it starts.119120```js121// main.js122import {execaNode} from 'execa';123124const ipcInput = [125 {task: 'lint', ignore: /test\.js/},126 {task: 'copy', files: new Set(['main.js', 'index.js']),127}];128await execaNode({ipcInput})`build.js`;129```130131```js132// build.js133import {getOneMessage} from 'execa';134135const ipcInput = await getOneMessage();

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

lines 136–137why, one step back
136```
137

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 138–141why, one step back
138## Message type
139140By 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).141

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

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 143–144why, one step back
143
144```js

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 145–146why, one step back
145import {execaNode} from 'execa';
146

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 147–148why, one step back
147const subprocess = execaNode({serialization: 'json'})`child.js`;
148```

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

line 149why, one step back
149

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

lines 150–151why, one step back
150## Messages order
151

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 152why, one step back
152The messages are always received in the same order they were sent. Even when sent all at once.

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 153why, one step back
153

Improve IPC documentation (#1079)ehmickybefore adoption4414737

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 156why, one step back
156

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 157–161why, one step back
157await Promise.all([
158 sendMessage('first'),159 sendMessage('second'),160 sendMessage('third'),161]);

Improve IPC documentation (#1079)ehmickybefore adoption4414737

lines 162–163why, one step back
162```
163

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 164why, one step back
164## Debugging

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 165why, one step back
165

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 166why, one step back
166When 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).

Improve IPC documentation (#1079)ehmickybefore adoption4414737

line 167why, one step back
167

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 168why, one step back
168Also, when the subprocess [failed](errors.md#subprocess-failure), [`error.ipcOutput`](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 169–174why, one step back
169
170<hr>171172[**Next**: 🐛 Debugging](debugging.md)\173[**Previous**: ⏳️ Streams](streams.md)\174[**Top**: Table of contents](../readme.md#documentation)