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.