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 fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–10why, one step back1<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 messages10Add `ipcInput` option (#1068)ehmickybefore adoption46cae30
line 11why, one step back11When 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 back1213The `ipc` option defaults to `true` when using [`execaNode()`](node.md#run-nodejs-files) or the [`node`](node.md#run-nodejs-files) option.14Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 15why, one step back15The 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 back16Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 17why, one step back17The 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 back18Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 19–21why, one step back19```js20// parent.js21import {execaNode} from 'execa';Add user guides (#989)ehmickybefore adoptionbe91ecf
line 22why, one step back22Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 23why, one step back23const subprocess = execaNode`child.js`;Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 24–25why, one step back24await subprocess.sendMessage('Hello from parent');25const message = await subprocess.getOneMessage();Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 26why, one step back26console.log(message); // 'Hello from child'Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 27–28why, one step back27```28Add user guides (#989)ehmickybefore adoptionbe91ecf
line 29why, one step back29```jsAdd promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 30why, one step back30// child.jsAdd `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 31why, one step back31import {getOneMessage, sendMessage} from 'execa';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 32why, one step back32Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 33–35why, one step back33const 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 back36```3738## Listening to messages39Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 40why, one step back40The 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 back4142[`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```jsAdd user guides (#989)ehmickybefore adoptionbe91ecf
lines 45–48why, one step back45// parent.js46import {execaNode} from 'execa';4748const subprocess = execaNode`child.js`;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 49–56why, one step back49await 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 back57```5859```js60// child.jsAdd promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 61why, one step back61import {sendMessage, getEachMessage} from 'execa';Add user guides (#989)ehmickybefore adoptionbe91ecf
line 62why, one step back62Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 63–65why, one step back63// 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 back66 break;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 67–71why, one step back67 }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 back72```73Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 74–93why, one step back74## Filter messages7576```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```93Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 94–95why, one step back94## Retrieve all messages95Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 96why, one step back96The [`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 back9798```js99// main.js100import {execaNode} from 'execa';101Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
lines 102–104why, one step back102const {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 back105```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 back114```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 back136```137Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 138–141why, one step back138## Message type139140By 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).141Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 142why, one step back142To 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 back143144```jsAdd `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 145–146why, one step back145import {execaNode} from 'execa';146Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 147–148why, one step back147const subprocess = execaNode({serialization: 'json'})`child.js`;148```Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 149why, one step back149Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
lines 150–151why, one step back150## Messages order151Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 152why, one step back152The 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 back153Improve IPC documentation (#1079)ehmickybefore adoption4414737
line 154why, one step back154```jsDebounce `message` events (#1087)ehmickybefore adoptione8bab97
line 155why, one step back155import {sendMessage} from 'execa';Improve IPC documentation (#1079)ehmickybefore adoption4414737
line 156why, one step back156Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 157–161why, one step back157await Promise.all([158 sendMessage('first'),159 sendMessage('second'),160 sendMessage('third'),161]);Improve IPC documentation (#1079)ehmickybefore adoption4414737
lines 162–163why, one step back162```163Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 164why, one step back164## DebuggingImprove IPC documentation (#1079)ehmickybefore adoption4414737
line 165why, one step back165Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 166why, one step back166When 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 back167Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 168why, one step back168Also, 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 back169170<hr>171172[**Next**: 🐛 Debugging](debugging.md)\173[**Previous**: ⏳️ Streams](streams.md)\174[**Top**: Table of contents](../readme.md#documentation)