2.0.4 • Published 6 years ago

@coupler/mid-mq v2.0.4

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

A coupler middleware implements functions for message producer.

it's implemented based on amqplib.

all tests passed on rabbitmq@3.6.11

#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:guest@127.0.0.1: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