rabbit-service
This service leverages RabbitMQ and generators to create a simple, extensible service for messaging. Functionality is built on top of the rabbit.js module.
Usage
Promises
let rs = ;let service = ; service; service; service ;
Generators
let co = ;let rs = ;let service = ; ;
Documentation
rs(config)
let rs = ;let service = ;
Accepts
- Url string to be deconstructed into host connection parameters
- Configuration object to overwrite the following defaults
Behavior
- Instance of a service
this
In most cases, this
will refer to the context of the request, and is shared across all functions called in the request
context: Context // rabbit.js client context channel: String // channel the message ws sent on socket: // socket config after being processed channel: String topic: String controller: Function // controller defined by the user _controller: Function // controller post processed listen: String type: String encoding: String options: {} // options accepted by the rabbit.js socket connection controller: String message: String // message sent to the socket
service.use(function)
let service = ; // generatorservice; // promiseservice
Accepts
- Either
- Generator function
- Function which returns a promise
Behavior
- Function parameter is added to the middleware chain
- Message event is processed in order that the middleware was added
- Function must call
next()
to progress to the next middleware function - The context
this
refers to the message event and is shared through the middleware chain
service.register
let service = ; service; service;
Accepts
- Either
- Socket config
- Array of configs
- Configs will be defaulted against the config default socket
- Configs must have a controller property that is either:
- Generator function
- Function returning a promise
- String specifying a controller
- Requires: Service config property
controller
to specify a path to a directory - If a property chain is used:
- The leading word (
.
delimited) will be the controller - The remaining string is used to traverse subproperties of the controller module
- Eg:
my_controller.foo[0]
my_controller
is the file in the controllers directory- The handler used will be the function found in the first element
[0]
of thefoo
property
- The leading word (
- Requires: Service config property
Behavior
- The config passed will be used to crete a socket via rabbit.js
- The
controller
property will be called after all middleware is called - The context of
this
refers to the message event and is shared through the middleware chain
service.catch(function)
let service = ; service; service;
Accepts
- Either
- Generator function
- Function returning a promise
- Function should expect a parameter
error
- Note:
this
is not guaranteed to be the context of a message event. Errors may occur outside the context of a message
Behavior
- The callback is called any time an error is thrown from the rabbit client
- For middleware/controller only error handling, use:
service; // OR service;
service.reconnect(function)
let service = ; service; // OR service
Accepts
- Either
- Generator or funciton which returns a promise to be treated as the handler
- Config to overwrite the default config, wich an optional
handler
function - Default:
start_delay: 1000 // start delay max_delay: 60000 // maximum delay multiplier: 2 // amount the delay is multiplied with each failure
Behavior
- If the connection is dropped, a reconnection loop with exponential backoff begins
- the handler function is called after every drop, and reconnection
service.listen(config)
let service = ; service;
Accepts
- Optional config for last minute overrides
- Config matches object passed to
rs(config)
Behavior
- Compiles all functions and handlers into a finalized state
- Creates client context
- Connects all sockets
- registers all event listeners
- NOTE: This function is called on every reconnect attempt
Authors
License
MIT