amqp-hutch
amqplib wrapper
Last updated 3 years ago by robinsio .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install amqp-hutch 
SYNC missed versions from official npm registry.

AMQP:Hutch

amqplib wrapper for easy setup and initialization.

Setup

Configuration and Events for AMQP Hutch.

var AMQPHutch = require('amqp-hutch');

var hutch = new AMQPHutch();

hutch.initialise({
  connectionString: 'amqps://user:password@host:port/uri?heartbeat=3',
  retryWait:        1000
});

hutch.on('ready', function() {
  console.log('Established RabbitMQ connection');
});

hutch.on('close', function(err) {
  console.log(err.message + 'RabbitMQ closing connection');
});

hutch.on('error', function(err) {
  console.log(err.message + 'RabbitMQ connection error');
});

module.exports = hutch;

Publish

var message = {"Message": "Hello"};

var options = {
  exchange: {
    durable: true,
    confirm: true,
    autoDelete: false,
    type: 'topic',
    name: 'example.exchange'
  },
  publish: {
    persistent: true,
    contentType: 'application/json',
    expiration: 86400000,
    timestamp: Math.floor(Date.now() / 1000)
  }
};

hutch.publish(options, message, function(err, res) {
  console.log(res);
});

Publish to Exchange

var message = {"Message": "Hello"};

var publishOptions = {
  exchange: {
    durable: true,
    confirm: true,
    autoDelete: false
  },
  publish: {
    persistent: true,
    contentType: 'application/json',
    expiration: 86400000,
    timestamp: Math.floor(Date.now() / 1000)
  }
};

hutch.publishToExchange('exchange.name', 'topic', publishOptions, message, function(err, res) {
  console.log(res);
});

Consume

Consume creates a queue bound to a new channel.


  var options = {
    exchange: {
      name: 'exchange.name',
      type: 'topic'
    },
    queue: {
      name: 'queue.name',
      prefetch: 1,
      durable:  true
    },
    routingKey: '#'
  };

  var consumer = function(message, done, fail) {
    some.service(message, function(err, res) {
      if(err) return fail();    
      done();
    });
  };

  hutch.consume(options, consumer, function(err) {
    console.log("Successfully setup consumer for queue: [" + options.queue + "]");
  });

Exclusive

Adding the Exclusive flag to the options will manage an exclusive consumer, the conusmer will retry until closed.


  var options = {
    exchange: {
      name: 'exchange.name',
      type: 'topic'
    },
    queue: {
      name: 'queue.name',
      prefetch: 1,
      durable:  true
    },
    exclusive: true
  };

SkipNext

Adding the skipNext flag to the options will skip the next message in the queue before initialising, this can be useful for unblocking failed messages.


  var options = {
    exchange: {
      name: 'exchange.name',
      type: 'topic'
    },
    queue: {
      name: 'queue.name',
      prefetch: 1,
      durable:  true
    },
    skipNext: true
  };

Destroy

Destroy will unbind/purge the queue from the given exchange.

  var queue    = "queue.name";
  var exchange = "exchange.name";

  hutch.destroy(queue, exchange, function(err) {
    console.log("Successfully unbound queue: [" + queue + "]");
  });

Monitoring

amqp-hutch offers two properties which may come in helpful for monitoring.

status

The status property can contain two values CONNECTED or DISCONNECTED.

configuration

The configuration object contains the original configuration passed to amqp-hutch.

  if (hutch.isConnected()){
    console.log(hutch.configuration.connectionString)
  }

Error Handing

Errors triggered from the underlinging service calls with RabbitMQ can be caught by listening for events.

hutch.on('error', function(err) {
  console.log(err);
});

If the service is invoked prior to a connection been established the service will return a 'AMQPConnectionError'

Current Tags

  • 1.0.20                                ...           latest (3 years ago)

28 Versions

  • 1.0.20                                ...           3 years ago
  • 1.0.19                                ...           3 years ago
  • 1.0.18                                ...           3 years ago
  • 1.0.17                                ...           3 years ago
  • 1.0.16                                ...           3 years ago
  • 1.0.15                                ...           3 years ago
  • 1.0.14                                ...           3 years ago
  • 1.0.13                                ...           3 years ago
  • 1.0.12                                ...           3 years ago
  • 1.0.11                                ...           4 years ago
  • 1.0.10                                ...           4 years ago
  • 1.0.9                                ...           4 years ago
  • 1.0.8                                ...           4 years ago
  • 1.0.7                                ...           4 years ago
  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.0.7                                ...           4 years ago
  • 0.0.6                                ...           4 years ago
  • 0.0.5                                ...           4 years ago
  • 0.0.4                                ...           4 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.2                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (9)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |