# open-channel

> A self-hosting inter-process communication channel for NodeJS.

Latest version **0.1.12** (published 2021-01-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install open-channel
pnpm add open-channel
yarn add open-channel
bun add open-channel
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.12 |
| Published | 2021-01-03 |
| First published | 2018-11-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 7 |
| Unpacked size | 30.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ayon Lee |
| Maintainers | hyurl |
| Keywords | multi-processing, ipc, self, server, socket |

## Links

- npm: https://www.npmjs.com/package/open-channel
- Repository: https://github.com/hyurl/open-channel
- Homepage: https://github.com/hyurl/open-channel#readme
- Issues: https://github.com/hyurl/open-channel/issues
- npm.io page: https://npm.io/package/open-channel

## Dependencies (7)

- [tslib](https://npm.io/package/tslib.md) ^1.9.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.1
- [@types/fs-extra](https://npm.io/package/@types/fs-extra.md) ^5.0.4
- [manager-process](https://npm.io/package/manager-process.md) ^0.1.3
- [is-socket-reset-error](https://npm.io/package/is-socket-reset-error.md) ^0.1.1
- [is-connect-refused-error](https://npm.io/package/is-connect-refused-error.md) ^0.1.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.1.12 (latest) — 2021-01-03
- 0.1.11 — 2019-07-18
- 0.1.10 — 2019-04-07
- 0.1.9 — 2019-02-20
- 0.1.8 — 2019-02-20
- 0.1.7 — 2018-12-08
- 0.1.6 — 2018-11-10
- 0.1.5 — 2018-11-10
- 0.1.4 — 2018-11-10
- 0.1.3 — 2018-11-10
- 0.1.2 — 2018-11-09
- 0.1.1 — 2018-11-09
- 0.1.0 — 2018-11-09

## README

# Open-Channel

**A self-hosting inter-process communication channel for NodeJS.**

## Design Purpose

Even if NodeJS provides its IPC communication abilities internally and there are
a lot third party packages that provides IPC functionality. However, all of them
require starting an IPC server or based on **cluster**/**child_process**, which 
are very unsuitable and may not work with the some situations:

1. The developer doesn't have authority to write any logic in the master process, 
    e.g. your app runs under [PM2](https://pm2.io) supervision.
2. If the IPC server is down, all communications will be lost and must manually 
    restart the server.
3. The developer must set exact protocol and net port (probably) to communicate,
    which may not be available when run-time environment is changed.

**Open-Channel is meant to resolve these problems.** With it, you will have
these advantages:

1. No need of master process accessibility, all code is written for a single 
    process (whether it will be run as child-process or individual process).
2. No need to start the IPC server manually, the channel will ship it internally.
3. Automatically switch the best protocol to transmit, on Unit, it uses domain
    socket, on Windows, it uses named pipe by default and changed to net port 
    when in cluster mode.
4. Automatically reconnect and resend messages if the connection is lost, always
    keep communications ongoing.
5. Sending messages even before the connection established, they will be queued
    and sent once connection is ready.

## Example

```javascript
const openChannel = require("open-channel").default;

var channel = openChannel(socket => {
    // This is a connection lister that passed to net.createServer().
    // The server instance will not be created until the first time calling 
    // channel.connect().
    // Put Your logic here to handle client connections and communications with 
    // the server.
});

// Gets a net.Socket instance connected to the server.
// you can pass an optional timeout argument in milliseconds to connect().
// The connection will not be immediately ready, but you still can send messages.
var socket = channel.connect();

// Put you logic here to communicate with the server.
```

## API

- `openChannel(connectionListener: (socket: net.Socket) => void): ProcessChannel`
- `openChannel(name: string, connectionListener: (socket: net.Socket) => void): ProcessChannel`
    - `name` The name of the channel. It's required to set a unique name of the 
        channel when you have multiple channels on the same machine.
    - `connectionListener` is set for `net.createServer()`.

- `channel.connect(timeout?: number): net.Socket` Gets the socket client that 
    will connect to the internal server.
    - `timeout` Default value is `5000`ms.

- `channel.name: string` The same name passed to `openChannel()`.

- `channel.state: string` Returns the status of the channel, which will either 
    be `initiated`, `connecting`, `connected` or `closed`.

- `channel.connected: boolean` Whether the channel is connected to the internal 
    server.

- `socket.write()` The client side write method has been re-written to implement
    sending messages even when the channel state is connecting.

- `socket.destroy()` The client side destroy method has been re-written to 
    implement closing both the channel and the socket. (The internal server will
    still be working, and currently unable to close the channel on server side.)

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