amqplib-rpc
Thin Amplib utils for RabbitMQ RPC in Node.js. Uses replyTo
and correlationId
message properties as the RabbitMQ rpc tutorial suggests.
Installation
npm i --save amqplib-rpc
Usage
request
Make an rpc request, publish a message to an rpc queue.
- Creates a channel, queue, correlationId, and sets up
properties.replyTo
andproperties.correlationId
on request message.
/** * @param * @param * @param * @param * @param * @param * @param * @param * @param * @param * @return */
request examples, rpc client
// Promise api examplevar amqplib = var request = request amqplib // Callback api examplevar amqplib = var request = request amqplib // Timeout Error example// triggered by timeout optionvar amqplib = var request = requestvar TimeoutError = TimeoutError amqplib // Channel Close Error example// occurs if channel is closed before the response is received from rpcvar amqplib = var request = requestvar ChannelCloseError = ChannelCloseError amqplib
reply
Reply to an rpc request, publish a message to replyTo queue.
-
Replies to a message using
properties.replyTo
andproperties.correlationId
. -
Reply will NOT error if the "replyTo" queue does not exist, if you need it to use
checkReplyTo
(example below). -
Reply will NOT
ack
themessage
. Ack/Nack must be done manually.
/** * @param * @param * @param * @param * @return */
reply example, rpc server
var amqplib = var reply = reply amqplib
checkReplyQueue
Create a channel, check if replyTo queue exists, and close the channel.
checkReplyQueue
creates it's own channel, bc checking for a non-existant queue errors and closes the channel.
/** * @param * @param * @param * @return */
checkReplyQueue before reply example, rpc server
var amqplib = var reply = replyvar checkReplyQueue = checkReplyQueuevar QueueNotFound = QueueNotFound amqplib
checkQueue
Create a channel, check if the queue exists, and close the channel. This is not rpc related but was implemented for checkReplyQueue
, so I've exported it.
In some cases, it may be useful to check if a queue exists before publishing to it.
/* * @param {AmqpblibConnection} connection amqplib rabbitmq connection * @param {String} queue queue name * @param {Function} [cb] callback, not required if using promises * @return {Promise} if using promises */
checkReplyQueue before reply example, rpc server
var amqplib = var reply = replyvar checkQueue = checkQueuevar QueueNotFound = QueueNotFound amqplib
Follows RabbitMQ RPC tutorial
https://www.rabbitmq.com/tutorials/tutorial-six-javascript.html
Changelog
License
MIT