Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rse/ipc-pubsub

Inter-Process-Communication (IPC) Publish-Subscribe (PubSub) Abstraction Layer
https://github.com/rse/ipc-pubsub

abstraction ipc layer publish pubsub subscribe

Last synced: 4 days ago
JSON representation

Inter-Process-Communication (IPC) Publish-Subscribe (PubSub) Abstraction Layer

Awesome Lists containing this project

README

        

IPC-PubSub
==========

Inter-Process-Communication (IPC) Publish-Subscribe (PubSub) Communication Abstraction Layer



About
-----

This [Node.js](https://nodejs.org) module provides an
abstraction layer for [Inter-Process-Communication
(IPC)](https://en.wikipedia.org/wiki/Inter-process_communication)
through Publish-Subscribe communication. It supports the following modes
(in order of increasing process scope and overall complexity):

- **Single-Process-Model (SPM):**

This is for Node applications NOT using the Node.js
[`cluster`](https://nodejs.org/api/cluster.html) module.
The communication is performed with an in-memory
[`PatternEmitter`](http://npmjs.com/pattern-emitter). No external
resource is needed.

- **Multi-Process-Model (MPM):**

This is for Node applications using the Node.js
[`cluster`](https://nodejs.org/api/cluster.html) module. Hence, it is
for Node applications split into distinct (related) processes, running
on the same machine. The communication is performed with an in-memory
[`PatternEmitter`](http://npmjs.com/pattern-emitter) in each process
and an IPC message exchange between the processes with the help of the
Node.js [`cluster`](https://nodejs.org/api/cluster.html) module. No
external resource is needed.

- **Remote-Process-Model (RPM):**

This is for Node applications split into (unrelated)
distinct processes, usually running on distinct machines.
The communication is performed with the help of an
external broker. Currently a [NATS](https://nats.io/)
broker (e.g. [gNATSd](https://github.com/nats-io/gnatsd)),
a [MQTT](http://mqtt.org/) broker (e.g.
[Mosquitto](https://mosquitto.org/)), [Redis](https://redis.io/)
[Pub/Sub](https://redis.io/topics/pubsub)
or [PostgreSQL](https://www.postgresql.org/)
[LISTEN](https://www.postgresql.org/docs/current/static/sql-listen.html) /
[NOTIFY](https://www.postgresql.org/docs/current/static/sql-notify.html)
is supported.

Installation
------------

```shell
$ npm install ipc-pubsub --save-dev
```

Usage
-----

```js
(async () => {
const PubSub = require("ipc-pubsub")

/* open connection */
let pubsub = new PubSub("spm")
await pubsub.open()

/* subscribe for messages */
await pubsub.subscribe("foo/#", (value, channel) => {
console.log(value, channel) // -> "quux" "foo/bar"
})

/* publish message */
await pubsub.publish("foo/bar", "quux")

/* close connection */
await pubsub.close()
})()
```

The following URLs are supported on `new PubSub(url)`:

- `spm:`
- `mpm:`
- `rpm+redis://[xxx:@][:][/]`
- `rpm+mqtt://[:@][:][/][?tls=true[&&key=&&crt=&&ca=]]`
- `rpm+nats://[:@][:][/][?tls=true[&&key=&&crt=&&ca=]]`
- `rpm+pgsql://[:@][:][/][?tls=true[&&key=&&crt=&&ca=]]`

The `` is an arbitrary unique identifier matching the regular expression `^[a-zA-Z][a-zA-Z0-9-]*$`.

The channel names are MQTT-style topic names, i.e., slash-separated strings
like `foo/bar/quux`. The channel argument of `subscribe(channel, ...)`
is actually an MQTT-style topic pattern, i.e., it can contain `*` for single
element and `#` for remaining elements. For example: `foo/bar/*/quux/#`
will match `foo/bar/baz/quux/foo`. The only exception is the PostgreSQL
mechanism: it does not support any MQTT-style wildcards at all.

Recommendation: As Redis does not support TLS and PostgreSQL does not support
MQTT-style wildcards, we prefer to use IPC-PubSub primarily with MQTT and
[Mosquitto](https://mosquitto.org/) or NATS and [gNATSd](https://github.com/nats-io/gnatsd).

Application Programming Interface (API)
---------------------------------------

```ts
interface PubSubSubscription {
unsubscribe(): Promise;
}

declare class PubSub {
constructor(url: string);

open(): Promise;

publish(
channel: string,
message: any
): Promise;

subscribe(
channelPattern: string,
onMessage: (message: any, channel: string) => void
): Promise;

close(): Promise;
}
```

License
-------

Copyright (c) 2017-2019 Dr. Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.