A Graft microservice that implements a pub/sub broker.
npm install graft-broker --save
var broker = graftBroker()
, updates = broker.ReadChannel()
, ret = broker.ReadChannel()
ret.on('data', function(data) {
console.log('subscription result', data)
// prints
// subscription result { status: 'subscribed', topic: 'hello' }
})
updates.on('data', function(data) {
console.log('message received', data)
// prints
// message received { cmd: 'publish',
// topic: 'hello',
// hello: 'world' }
})
broker.write({
cmd: 'subscribe'
, topic: 'hello'
, messages: updates // mandatory channel for updates
, ret: ret // optional return channel
})
broker.write({
cmd: 'publish'
, topic: 'hello'
, hello: 'world'
})
The subscribe command will return an error on the return channel if there is no topic or no messages channel. The error format follows the pattern:
{
status: 'not subscribed'
, reason: 'missing topic'
}
MIT