# @slimio/ipc

> Node.js Inter Process Communication

Latest version **0.2.1** (published 2019-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @slimio/ipc
pnpm add @slimio/ipc
yarn add @slimio/ipc
bun add @slimio/ipc
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2019-12-17 |
| First published | 2019-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 3 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | SlimIO |
| Maintainers | ahkrin, alexandre.malaj, captainfive, fraxken, mcroquet |
| Keywords | SlimIO, ipc, process, communication, wrapper |

## Links

- npm: https://www.npmjs.com/package/@slimio/ipc
- Repository: https://github.com/SlimIO/ipc
- Homepage: https://github.com/SlimIO/ipc#readme
- Issues: https://github.com/SlimIO/ipc/issues
- npm.io page: https://npm.io/package/@slimio/ipc

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [@slimio/is](https://npm.io/package/@slimio/is.md) ^1.5.1
- [@slimio/safe-emitter](https://npm.io/package/@slimio/safe-emitter.md) ^1.1.0

## Recent versions

- 0.2.1 (latest) — 2019-12-17
- 0.2.0 — 2019-12-17
- 0.1.3 — 2019-06-30
- 0.1.2 — 2019-03-24
- 0.1.1 — 2019-02-09
- 0.1.0 — 2019-01-04

## README

# ipc
![stability-unstable](https://img.shields.io/badge/stability-unstable-yellow.svg)
![version](https://img.shields.io/badge/dynamic/json.svg?url=https://raw.githubusercontent.com/SlimIO/ipc/master/package.json&query=$.version&label=Version)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/SlimIO/ipc/commit-activity)
![MIT](https://img.shields.io/github/license/mashape/apistatus.svg)
![dep](https://img.shields.io/david/SlimIO/ipc)
![size](https://img.shields.io/bundlephobia/min/@slimio/ipc)
[![Known Vulnerabilities](https://snyk.io//test/github/SlimIO/ipc/badge.svg?targetFile=package.json)](https://snyk.io//test/github/SlimIO/ipc?targetFile=package.json)
[![Build Status](https://travis-ci.com/SlimIO/ipc.svg?branch=master)](https://travis-ci.com/SlimIO/ipc)
[![Greenkeeper badge](https://badges.greenkeeper.io/SlimIO/ipc.svg)](https://greenkeeper.io/)

Node.js end-to-end IPC (Inter Process Communication). The goal of this package is to reduce the code required to implement an interaction between two Node.js process.

## Features
- Send message with subjects (allow you to build comprehensive code).
- Support streaming communication in both-end

## Requirements
- [Node.js](https://nodejs.org/en/) v12 or higher

## Getting Started

This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).

```bash
$ npm i @slimio/ipc
# or
$ yarn add @slimio/ipc
```

## Usage example

Create a `master.js` file with the following code:
```js
// Node.js Dependencies
const { fork } = require("child_process");
const { join } = require("join");
const { strictEqual } = require("assert");

// Third-party Dependencies
const IPC = require("@slimio/ipc");

async function main() {
    const cp = fork(join(__dirname, "worker.js"));
    const master = new IPC(cp);
    strictEqual(master.isMaster, true);

    const ret = await master.send("sayHello", "bob");
    strictEqual(ret, "hello bob");

    cp.disconnect();
}
main().catch(console.error);
```

And now create a `worker.js` file at the same location with the following code:
```js
const { strictEqual } = require("assert");
const IPC = require("@slimio/ipc");

const slave = new IPC();
strictEqual(slave.isMaster, false);

slave.on("sayHello", (nickName, next) => {
    // use next() to send a response!
    next(`hello ${nickName}`);
});
```

## API
You have to subscribe to the event-emitter to get incomming messages.

### constructor(cp?: ChildProcess)
Create a new IPC instance. Take a Node.js ChildProcess instance when the script is "**Master**". Creating an IPC as slave on a master script will throw an Error.

### send(subject: String, data: any): Promise< any >
Send a message to the master or slave (depending on the side).

### on(subject: string, subscriber: (payload: any, next) => void): void
Subscribe to a given subject.

```js
ipc.on("subject", (payload, next) => {
    console.log("received subject!");
    next();
});
```

## Stream communication
SlimIO IPC bring support for stream communication.

> TBC

## Roadmap
- Implement Node.js workers!

## Dependencies

|Name|Refactoring|Security Risk|Usage|
|---|---|---|---|
|[@slimio/is](https://github.com/SlimIO/is#readme)|Minor|Low|Type Check|
|[@slimio/safe-emitter](https://github.com/SlimIO/safeEmitter#readme)|Minor|Medium|Safe Node.js Emitter (error isolation)|
|[uuid](https://github.com/kelektiv/node-uuid#readme)|Minor|Low|Unique ID|

## License
MIT

---
_Source: https://npm.io/package/@slimio/ipc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
