# pub-sub-js

> Distributed publish/subscribe messaging system

Latest version **1.1.0** (published 2014-08-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install pub-sub-js
pnpm add pub-sub-js
yarn add pub-sub-js
bun add pub-sub-js
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2014-08-10 |
| First published | 2014-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | x25 |
| Maintainers | x25 |
| Keywords | pub, sub, pubsub, publish, subscribe, distributed, network, messaging |

## Links

- npm: https://www.npmjs.com/package/pub-sub-js
- Repository: http://github.com/x25/pub-sub-js
- Homepage: https://github.com/x25/pub-sub-js
- Issues: https://github.com/x25/pub-sub-js/issues
- npm.io page: https://npm.io/package/pub-sub-js

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2014-08-10
- 1.0.1 — 2014-06-02
- 0.1.9 — 2014-06-01
- 0.1.8 — 2014-04-12
- 0.1.5 — 2014-02-11
- 0.1.4 — 2014-01-20
- 0.1.2 — 2014-01-15
- 0.0.1 — 2014-01-11

## README

pub-sub-js [![](https://travis-ci.org/x25/pub-sub-js.png)](https://travis-ci.org/x25/pub-sub-js) [![](https://coveralls.io/repos/x25/pub-sub-js/badge.png)](https://coveralls.io/r/x25/pub-sub-js)
==========
**pub-sub-js** is tiny library with zero dependencies that implements distributed publish/subscribe messaging system.

```bash
$ npm install pub-sub-js
```

Why not use zeromq ? Because of this [bug](https://github.com/JustinTulloss/zeromq.node/issues/272).

## Usage

Publish messages to channels:

```js
var Pub = require('pub-sub-js').Publisher;

var publisher = new Pub();

publisher.listen(8002);

setInterval(function () {

  publisher.publish('channel_one', 'message_one');
  publisher.publish('channel_two', {message: 'two'});
  publisher.publish('channel_three', ['message', 'three']);

}, 1000);
```

Subscribe to channels and receive the messages:

```js
var Sub = require('pub-sub-js').Subscriber;

var subscriber = new Sub();

subscriber.subscribe('channel_one');
subscriber.connect(8002, '127.0.0.1');
subscriber.subscribe('channel_two');

subscriber.on('message', function (channel, data) {

  console.log(channel, data);
});
```

Output:

```
channel_one message_one
channel_two { message: 'two' }
```

Message three will not be received because there is no subscription to the channel_three. There will be **no network traffic** on publisher.send('channel_three', ...) call.

## API

###Class: Subscriber

**.subscribe(channel)**

Subscribes to the channel.

**.unsubscribe(channel)**

Unsubscribes from the channel.

**.connect(port, [host])**

Opens connection to the Publisher. See [net.Socket.connect()][node-api-net].

**.disconnect(port, [host])**

Closes the connection.

**.listen(port, [host])**

Begin accepting connections on the specified port and hostname. See [net.Server.listen()][node-api-net].

**.close([callback])**

Stops from accepting new connections. See [net.Server.close()][node-api-net].

**Event: 'message' (channel, data)**

Emitted when the message is received.

**Event: 'log' (args)**

Can be used for debugging.

###Class: Publisher

**.publish(channel, message)**

Publish message to the channel.

**.connect(port, [host])**

Opens connection to the Subscriber. See [net.Socket.connect()][node-api-net].

**.disconnect(port, [host])**

Closes the connection.

**.listen(port, [host])**

Begin accepting connections on the specified port and hostname. See [net.Server.listen()][node-api-net].

**.close([callback])**

Stops from accepting new connections. See [net.Server.close()][node-api-net].

**Event: 'log' (args)**

Can be used for debugging.

## Tests

```sh
$ npm test
```

## License

MIT

[node-api-net]: http://nodejs.org/api/net.html

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