mq-reconnect

0.6.1 • Public • Published

MQ-Reconnect

This package is one simple wrapper for amqplib.

Document

MQ-Reconnect is the wrapper for amqplib. It makes send or publish message simple, and using persistent connection, suporting reconnect to server when connection closed.

Client

create a client used to publish or send message or bind consumer to queue.

Client(name, url, [interval, confirm, logger, socketOptions])

Arguments:

​ name: client name. default: random string six length

​ url: connect url for rabbitmq. reference. default: amqp://localhost

​ interval: reconnect time when connection closed. default: 2000

​ confirm: boolean, whether using confirm channel. default: true

​ logger: logging object having info, error, warning functions. default: console

​ socketOptions: reference

publish message to exchange
Client.publish(ex, key, msg, type, exOptions, msgOptions)

Arguments:

​ exchange: exchange name.

​ key: routing key

​ msg: string or Object message

​ type: exchange type.

​ exOptions: assertExchange options

​ msgOptions: publish or sendToQueue options

send message to queue
Client.send(queue, msg, queueOptions, msgOptions)

Arguments:

​ queue: queue name.

​ msg: string or Object message

​ queueOptions: assertQueue options

​ msgOptions: publish or sendToQueue options

bind consumer to queue
Client.consume(queue, handle, options, reconnect)

Arguments

​ queue: queue name

​ handle: message handle function(msg, channel)

​ options: consume options. example: { noAck: false, prefetch: 1}

​ reconnect: whether reconnect when connection close. default: true

close the client
Client.close()

close the rabbitmq connection and channel.

Examples

  1. Create connect client

    const Client = require('mq-reconnect');
    
    var client = new Client('DemoClient', 'amqp://localhost');
  2. Send message to Queue

    client
        .send('queue_name', 'there is message')
        .then(function(info){
           // info is the message tracking records
           // like: ['put buffer', 'wait confirm', 'server acked']
        })
        .catch(function(err){
        	// process err
    	})
  3. Publish message to Exchange

    client
      .publish('exchange_name', 'route_key', 'there is message', 'topic')
      .then(function(info){
         // info is the message tracking records
         // like: ['put buffer', 'wait confirm', 'server acked']
      })
      .catch(function(err){
         // process err
      })
  4. Bind consumer to Queue

    client
       .consume('queue_name', function handle(msg, ch) {
           console.log(msg.content.toString());
           return ch.ack(msg);
         },
         { noAck: false, prefetch: 1}
       )
       .catch(err => {
         console.log(err.message);
       });

Readme

Keywords

Package Sidebar

Install

npm i mq-reconnect

Weekly Downloads

0

Version

0.6.1

License

MIT

Unpacked Size

18 kB

Total Files

10

Last publish

Collaborators

  • c_geek