# tw-pubsub

> Taskworld Pubsub module

Latest version **1.0.7** (published 2016-08-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install tw-pubsub
pnpm add tw-pubsub
yarn add tw-pubsub
bun add tw-pubsub
```

## 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.0.7 |
| Published | 2016-08-25 |
| First published | 2016-05-16 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Known vulnerabilities | 0 (+6 in 4 direct dependencies) |
| Install scripts | no |
| Author | Chakrit Likitkhajonr |
| Maintainers | chrisza-tw |

## Links

- npm: https://www.npmjs.com/package/tw-pubsub
- Repository: https://github.com/taskworld/tw-pubsub
- Homepage: https://github.com/taskworld/tw-pubsub#readme
- Issues: https://github.com/taskworld/tw-pubsub/issues
- npm.io page: https://npm.io/package/tw-pubsub

## Dependencies (10)

- [joi](https://npm.io/package/joi.md) ^8.1.0
- [lab](https://npm.io/package/lab.md) ^10.5.1
- [code](https://npm.io/package/code.md) ^2.3.0
- [uuid](https://npm.io/package/uuid.md) ^2.0.2
- [redis](https://npm.io/package/redis.md) ^2.6.0-2
- [tcomb](https://npm.io/package/tcomb.md) ^3.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.12.0
- [bluebird](https://npm.io/package/bluebird.md) ^3.3.5
- [rabbit.js](https://npm.io/package/rabbit.js.md) ^0.4.4
- [serialize-javascript](https://npm.io/package/serialize-javascript.md) ^1.3.0

## Recent versions

- 1.0.7 (latest) — 2016-08-25
- 1.0.6 — 2016-08-24
- 1.0.5 — 2016-08-22
- 1.0.4 — 2016-07-12
- 1.0.3 — 2016-06-27
- 1.0.2 — 2016-06-24
- 1.0.1 — 2016-05-16
- 1.0.0 — 2016-05-16

## README

## TW-Pubsub

Simple redis-pubsub system

#### Installation

```
npm install tw-pubsub
```

#### Initialize pub-sub

You can initialize by calling `pubsub.init(ChannelName)`. Channel name is set to default as `TW_MESSAGE_BUS`

You will get a channel after init pubsub system, which you can publish or subscribe to topics.

Example: 

```
const TwPubSub = require('tw-pubsub')
TwPubSub.pubsub.init(channel => {
  channel.publish('topic', { payload: 'some payload' })
  channel.subscribe('topic', (message) => {
    // Do some thing with message
  })
})
```

Message schema is defined as:

- sourceId = service name
- payloadId = unique id for this payload (use for reference)
- topic
- payload
- type = type of message. Right now we have 'request', 'response', 'others', 'errors'
- requestId = for request-response paradigm, identified payloadId which you will response to

#### Endpoints helper

If you want to use library as request-response paradigm, you can use endpoint helper to create endpoint for topic

Example:

```
TwPubSub.createPubsubEndpoint({
  topic: 'test1',
  handler: (message) => {
    return new Promise((resolve, reject) => {
      resolve('hello world')
    })
  },
  channel
})
```

`createPubSubEndpoint` require 3 options:

- topic = topic you want to handle
- handler = handle function (must be promise based)
- channel = channel object of pubsub system you want to create endpoint.

#### Request helper

You can create request to endpoint by using request helper.

Example:

```
TwPubSub.request('test-endpoint', { payload: 'test-payload' }, channel).then(message => {
  // Do something with message
})
```

You will get response from that endpoint, in message schema format.

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