New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

exp-amqp-connection

Package Overview
Dependencies
Maintainers
4
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exp-amqp-connection

[![Build Status](https://travis-ci.org/ExpressenAB/exp-amqp-connection.svg?branch=master)](https://travis-ci.org/ExpressenAB/exp-amqp-connection)

1.2.5
Source
npm
Version published
Weekly downloads
37
117.65%
Maintainers
4
Weekly downloads
 
Created
Source

Simple amqp library

Build Status

Features

  • Hides underlying amqp implementation details
  • Publish
  • Subscribe
  • Optionally kill process on errors
  • Reuse connection
  • Dead letter exchange handling

API

A single function is exported:

var amqpConn = require("exp-amqp-connection");

amqpConn({host: "amqphost"}, {reuse: "myKey", exchange: "myExchange"}, function (err, conn) {
  if (err) return console.err(err);
  ...
});

The first arg is amqp connection options. See https://github.com/postwait/node-amqp#connection-options-and-url.

The second arg defines various behaviour options:

var behaviourOpts = {
  dieOnError: "...", // If true, kill the node process in case of amqp errors
  exchange: "...", // Name of exchange to use. Leave undefined for rabbit default exchange.
  reuse: "...", // Reuse connections using the specified key
  logger: "..." // one-arg-function used for logging errors. Defaults to console.log
  deadLetterExchangeName: "...", // Enable dead letter exchange by setting a name for it.
  exchangeOptions: "...", // Options to pass to the exchange
  queueOptions: "...", // Options to pass to the queue
  subscribeOptions: "...", // Options to use for subscribing,
};

default values for options, these will be merged with your changes.

var defaultExchangeOptions = {
  durable: true,
  autoDelete: false,
  confirm: true
};
var defaultQueueOptions = {
  autoDelete: true
};
var defaultSubscribeOptions = {};

Examples

Publish

var amqpConn = require("exp-amqp-connection");

amqpConn({host: "amqpHost"}, {exchange: "myExchange"}, function (err, conn) {
  if (err) return console.err(err);
  conn.publish("myRoutingKey", "a message");
});

Subscribe

var amqpConn = require("exp-amqp-connection");

amqpConn({host: "amqpHost"}, {exchange: "myExchange"}, function (err, conn) {
  if (err) return console.err(err);
  conn.subscribe("myRoutingKey", "myQueueName", function (message, headers, deliveryInfo, messageObject) {
    console.log("Got message", message);
  });
});

Reuse connection

All calls providing the same reuse key will get the same connection returned. If no reuse key is provided, a new connection is returned each time.

The following will yield a single connection to rabbit instead of 5000:

var amqpConn = require("exp-amqp-connection");

for(var i = 0; i < 5000; i++) {
  amqpConn({host: "amqpHost"}, {reuse: "someKey"}, function (err, conn) {
    if (err) return console.err(err);
    conn.publish("myRoutingKey", "a message");
  });
}

Die on error

In certain cases you want to crash the entire node process when there is a problem with the amqp connection. For example durable subscriptions have problems recovering in certain corner cases, so in order to not risk getting into a deadlocked state it is better to crash and let the process restart.

var amqpConn = require("exp-amqp-connection");
amqpConn({host: "amqphost"}, {dieOnError: true}, function (err, conn) {
  if (err) return console.err(err);
  ...
});

Dead letter exchange

Messages from a queue can be 'dead-lettered'; that is, republished to another exchange. For more information: https://www.rabbitmq.com/dlx.html This option will create a dead letter queue with the name deadLetterExchangeName + ".deadLetterQueue"

var amqpConn = require("exp-amqp-connection");

var options = {
  exchange: "myExchange",
  deadLetterExchangeName: "myExchange.dead",
  subscribeOptions: {
    ack: true // Ack must be enabled for dead lettering to work
  }
}

amqpConn({host: "amqpHost"}, options, function (err, conn) {
  if (err) return console.err(err);
  conn.subscribe("myRoutingKey", "myQueueName", function (message, headers, deliveryInfo, messageObject) {
    if (message) {
      messageObject.acknowledge();
    } else {
      messageObject.reject(false); // reject=true, requeue=false causes dead-lettering
    }

    console.log("Got message", message);
  });
});

Keywords

amqp

FAQs

Package last updated on 19 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts