harmonia
v2.1.0
Published
RabbitMQ-backed RPC server
Downloads
114
Readme
Harmonia
Harmonia is a RabbitMQ-backed RPC server. Its API is inspired by HapiJS and is designed to work with many of the Hapi components (currently Joi and Boom are supported for validation and errors, respectively).
API
Harmonia
#constructor
options
- a configuration objectamqpUrl
- e.g.amqp://username:password@localhost:5672
socketOptions
- any socket option that can be passed toamqplib.connect
#route
route
- An object of the following format representing a method handlermethod
- A string representing the RPC method name (will also be the queue name)bindToExcahnges
- An array of objects describing exchanges to bind to. Warning: the exchange will be asserted, so make sure any existing exchanges on your RMQ server have matching settings.exchange
- the name of the exchange to bind your queue totype
- the type of the exchange (direct
,fanout
,topic
,headers
)routingKey
- the routing key to bind to the exchange with
deadLetterExchange
- the dead letter exchange for this queueconcurrency
- This is the AMQP prefetch (how many messages will be delivered from the queue at once)config
- an object containing the configuration for the handleronError
- one ofack
,nack
, orrequeue
. This is the action taken on a message when a runtime error occurs that reaches Harmoniahandler
- your method handler. This function must return a promise. Receives aRequest
object as its only parameter.validate
- can either be aJoi
object or a function (receives the incoming message as its only parameter)- if a function is provided, it should return true or undefined to indicate successful validation. Throwing any error or returning any value other than true will be treated as a validation failure.
invalidMessageHandler
- allows you to react to invalid messages (or override their default handling)
#listen
- Start listening to the configured queues on the given AMQP server#shutdown
- Stop handling new messages and disconnect (any requests in progress will be completed)
Client
#constructor
channel
- an established AMQP channeloptions
- an object. Currently, the only option istimeout
for RPC-style calls (timeout in ms or false for no timeout)
#createClient
- client factory; gives you a client to work with and disposes it (and the channel and connection when finished)amqpUrl
- used to create a connection to the servercallback
- a function that will receive the establishedclient
object. Must return a promise. The client will be disposed when this promise is resolved.
#createConnection
- create a disposable AMQP connection (for use with Bluebird'sPromise.using
)#createChannel
- creates a disposable channel on the given connection (again, for use with Bluebird'sPromise.using
)#awaitMethod
- RPC-style method call. DO NOT use this method for multiple calls to the same method.method
- the rpc method to callparams
- the parameters to the rpc methodoptions
- any options that can be sent to amqplib'sChannel#sendToQueue
#call
- deprecated - alias of#awaitMethod
#awaitMethodBulk
- same asawaitMethod
exceptparams
is an array of objects, each item in the array being a separate invokation#invokeMethod
- invoke a method, but don't wait for a response (promise resolves when the message broker has received the message)
Note on timeouts: you can set a default timeout by overriding Client.defaultTimeout
.
The default timeout is initialized to false in order to preserve backward-compatibility, but it
is highly recommended to set this to a value, even if it is long. If a remote service nacks a message
or simply fails to reply, you will leak memory, both in your Harmonia application and on the
RabbitMQ server (a reply queue, a channel, and a connection that will not be cleaned up until your
application restarts).
Examples
See the examples
directory