1.2.3 • Published 4 years ago

ipc-pubsub v1.2.3

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

IPC-PubSub

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

About

This Node.js module provides an abstraction layer for Inter-Process-Communication (IPC) 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 module. The communication is performed with an in-memory PatternEmitter. No external resource is needed.

  • Multi-Process-Model (MPM): This is for Node applications using the Node.js cluster 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 in each process and an IPC message exchange between the processes with the help of the Node.js cluster 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 broker (e.g. gNATSd), a MQTT broker (e.g. Mosquitto), Redis Pub/Sub or PostgreSQL LISTEN / NOTIFY is supported.

Installation

$ npm install ipc-pubsub --save-dev

Usage

(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:<id>
  • mpm:<id>
  • rpm+redis://[xxx:<secret>@]<host>[:<port>][/<id>]
  • rpm+mqtt://[<username>:<password>@]<host>[:<port>][/<id>][?tls=true[&&key=<file>&&crt=<file>&&ca=<file>]]
  • rpm+nats://[<username>:<password>@]<host>[:<port>][/<id>][?tls=true[&&key=<file>&&crt=<file>&&ca=<file>]]
  • rpm+pgsql://[<username>:<password>@]<host>[:<port>][/<id>][?tls=true[&&key=<file>&&crt=<file>&&ca=<file>]]

The <id> 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 or NATS and gNATSd.

Application Programming Interface (API)

interface PubSubSubscription {
    unsubscribe(): Promise<void>;
}

declare class PubSub {
    constructor(url: string);

    open(): Promise<void>;

    publish(
        channel: string,
        message: any
    ): Promise<void>;

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

    close(): Promise<void>;
}

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.

1.2.3

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.9.14

6 years ago

0.9.13

7 years ago

0.9.12

7 years ago

0.9.11

7 years ago

0.9.10

7 years ago

0.9.9

7 years ago

0.9.8

7 years ago

0.9.7

7 years ago

0.9.6

7 years ago

0.9.5

7 years ago

0.9.4

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago