https://github.com/compulim/childprocess-messageport
Turns ChildProcess IPC into MessagePort
https://github.com/compulim/childprocess-messageport
Last synced: 4 days ago
JSON representation
Turns ChildProcess IPC into MessagePort
- Host: GitHub
- URL: https://github.com/compulim/childprocess-messageport
- Owner: compulim
- Created: 2018-05-14T03:06:45.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-19T22:48:02.000Z (almost 7 years ago)
- Last Synced: 2025-02-17T13:11:44.573Z (3 months ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# childprocess-messageport
[](https://badge.fury.io/js/childprocess-messageport) [](https://travis-ci.org/compulim/childprocess-messageport)
Turns [`ChildProcess`](https://nodejs.org/api/child_process.html) IPC into [MessagePort](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort).
# Background
Instead of learning/using different API for different communication channels, we should unite them into a single interface pattern, either [MessagePort](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) or [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
# How to use
```js
const childProcess = ChildProcess.fork('echo.js');
const messagePort = new ChildProcessMessagePort(childProcess);messagePort.onmessage = event => {
// Could be either a string or Buffer
console.log(event.data);
};messagePort.postMessage('Hello, World!');
```> Instead of subscribing to `onmessage`, you can also subscribe using `on('message', handler)`.
If the underlying `childProcess.send` is failed, an `error` event will be emitted.
# Contributions
Like us? [Star](https://github.com/compulim/childprocess-messageport/stargazers) us.
Want to make it better? [File](https://github.com/compulim/childprocess-messageport/issues) us an issue.
Don't like something you see? [Submit](https://github.com/compulim/childprocess-messageport/pulls) a pull request.