@twilson63/palmetto-rmq
Advanced tools
Comparing version 1.0.5 to 1.1.0
16
index.js
@@ -0,8 +1,10 @@ | ||
var servicebus = require('servicebus') | ||
var EventEmitter = require('events').EventEmitter | ||
var ee = new EventEmitter() | ||
var servicebus = require('servicebus') | ||
module.exports = function (config) { | ||
var ee = new EventEmitter() | ||
// validate config | ||
if (!config.endpoint) throw new Error('endpoint required!') | ||
if (!config.endpoint) throw new Error('endpoint required!') | ||
if (!config.app) throw new Error('app required!') | ||
@@ -14,5 +16,7 @@ | ||
}) | ||
bus.subscribe(config.app, notify) | ||
if (!config.publishOnly) { | ||
config.roundRobin ? bus.listen(config.app, notify) : bus.subscribe(config.app, notify); | ||
} | ||
function notify (event) { | ||
@@ -23,5 +27,5 @@ if (event.to) ee.emit(event.to, event) | ||
ee.on('send', function (event) { | ||
bus.publish(config.app, event) | ||
config.roundRobin ? bus.send(config.app, event) : bus.publish(config.app, event); | ||
}) | ||
return ee | ||
} |
{ | ||
"name": "@twilson63/palmetto-rmq", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
107
README.md
# Palmetto RMQ | ||
This module uses rabbitMQ as the pub/sub messaging component for palmetto flow applications | ||
This module uses rabbitMQ as the pub/sub messaging component for palmetto flow applications. | ||
The default behavior works well if you want to have several subscribers that receive all messages. | ||
Optional behavior is to utilize the `roundRobin` option to distribute the message to the subscribers in a round-robin fashion. | ||
In either configuration you can optionally provide a `publishOnly` option. The returned instance will not receive any messages. | ||
[![Build Status](https://travis-ci.org/twilson63/palmetto-rmq.svg?branch=master)](https://travis-ci.org/twilson63/palmetto-rmq) | ||
@@ -67,2 +74,98 @@ | ||
The service listens to the `widget.all.request` svc then uses the `from` node to publish the response to. | ||
The service listens to the `widget.all.request` svc then uses the `from` node to publish the response to. | ||
------------------ | ||
### Fetching response from scaled service | ||
#### Requesting from a scaled load-balanced service instance | ||
``` js | ||
//requestor-service.js | ||
var requestorIo = require('@twilson63/palmetto-rmq')({ | ||
endpoint: 'amqp://guest:guest@localhost:5672', | ||
app: '<requestorServiceName>', | ||
vhost: '<optional>' | ||
}) | ||
var handlerIo = require('@twilson63/palmetto-rmq')({ | ||
endpoint: 'amqp://guest:guest@localhost:5672', | ||
app: '<handlerServiceName>', | ||
vhost: '<optional>', | ||
roundRobin: true, | ||
publishOnly: true // only the 'handlerService' will listen for messages | ||
}) // this instance shouldn't participate in `roundRobin` consumption | ||
var uuid = uuid.v4() | ||
//setup response msg handler | ||
requestorIo.on(uuid, function (event) { | ||
console.log(event.object) | ||
}) | ||
//send request msg | ||
handlerIo.emit('send', { | ||
to: 'widget.all.request', | ||
from: uuid, | ||
subject: 'widget', | ||
verb: 'all', | ||
type: 'request', | ||
object: {} | ||
}) | ||
``` | ||
#### Handling request in scaled load-balanced service | ||
``` js | ||
//handler-service.js | ||
//multiple instances of this service running | ||
var inboundIo = require('@twilson63/palmetto-rmq')({ | ||
endpoint: 'amqp://guest:guest@localhost:5672', | ||
app: '<handlerServiceName>', | ||
vhost: '<optional>', | ||
roundRobin: true, // all instances of 'handlerService' will get 'roundRobin' msg distribution | ||
publishOnly: false // and obviously listen for those msgs | ||
}) | ||
var requestorIo = require('@twilson63/palmetto-rmq')({ | ||
endpoint: 'amqp://guest:guest@localhost:5672', | ||
app: '<requestorServiceName>', | ||
vhost: '<optional>' | ||
}) | ||
//handle request messages | ||
inboundIo.on(uuid, function (event) { | ||
doSomething(event.object) | ||
.then(resultObject => { | ||
requestorIo.emit('send', { | ||
to: event.from, | ||
from: event.to, | ||
subject: event.subject + '-response', | ||
verb: event.verb + '-response', | ||
type: 'response', | ||
object: resultObject | ||
}) | ||
}) | ||
.catch(error => { | ||
requestorIo.emit('send', { | ||
to: event.from, | ||
from: event.to, | ||
subject: event.subject + '-error', | ||
verb: event.verb + '-error', | ||
type: 'response', | ||
object: error | ||
}) | ||
}) | ||
}) | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7341
8
117
171
1