@coupler/mid-mq
v2.0.4
Published
A coupler middleware implements functions for message producer.
Downloads
5
Readme
A coupler middleware implements functions for message producer.
it's implemented based on amqplib
.
all tests passed on [email protected]
#Usage
In coupler context
mq
interface is injected in the coupler context.
async function middleware(ctx, next) {
// to send message to a queue
await ctx.mq.send({ hello: 'world' }, 'specific-queue');
// to send message to queues
await ctx.mq.send({ hello: 'world' }, ['queue1', 'queue2']);
// to publish message to an exchange
await ctx.mq.publish({ hello: 'world' }, 'routing.key', 'specific-exchange');
// to publish message to exchanges
await ctx.mq.publish({ hello: 'world' }, 'routing.key', ['exchange1', 'exchange2']);
// ...
await next();
}
Configuration
the config has structure like:
{
url: 'amqp://guest:[email protected]:4396?heartbeat=30', // connection string
options: { }, // optional, connection options
};
Serialization
Nothing will be done if content to send/publish is buffer. Other type of content will be stringified to JSON and then built to a buffer.
Errors
- AMQP_PUBLISH_FAILED
- AMQP_SEND_FAILED
- AMQP_MESSAGE_HANDLER_ERROR
- AMQP_NOT_CONNECTED
- AMQP_QUEUE_NOT_EXISTS
- AMQP_ERROR