ipc.md
This file
Path
docs/ipc.md
84 lines · 3.3 KiB · Markdown
Last changed by
Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
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 messages10Move API reference to its own page (#996)ehmickybefore adoption7cde496
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.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.14Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 15why, one step back15The 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 back16Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 17–19why, one step back17```js18// parent.js19import {execaNode} from 'execa';Add user guides (#989)ehmickybefore adoptionbe91ecf
line 20why, one step back20Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 21–25why, one step back21const subprocess = execaNode`child.js`;22console.log(await subprocess.getOneMessage()); // 'Hello from child'23await subprocess.sendMessage('Hello from parent');24```25Add user guides (#989)ehmickybefore adoptionbe91ecf
line 26why, one step back26```jsAdd promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 27–40why, one step back27// child.js28import {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```jsAdd user guides (#989)ehmickybefore adoptionbe91ecf
lines 41–44why, one step back41// parent.js42import {execaNode} from 'execa';4344const subprocess = execaNode`child.js`;Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 45–52why, one step back45await 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 back53```5455```js56// child.jsAdd promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 57why, one step back57import {sendMessage, getEachMessage} from 'execa';Add user guides (#989)ehmickybefore adoptionbe91ecf
line 58why, one step back58Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 59–67why, one step back59// 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–73why, one step back68```6970## Message type7172By 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).73Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 74why, one step back74To limit messages to JSON instead, the [`serialization`](api.md#optionsserialization) option can be set to `'json'`.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 75–84why, one step back7576```js77const subprocess = execaNode({serialization: 'json'})`child.js`;78```7980<hr>8182[**Next**: 🐛 Debugging](debugging.md)\83[**Previous**: ⏳️ Streams](streams.md)\84[**Top**: Table of contents](../readme.md#documentation)