npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-rabbitmq-client

v3.0.2

Published

Auto-reconnect and round robin support for amqplib.

Downloads

65

Readme

Connection management for rabbitmq client

Node js Rabbit MQ client which has connection management backed into it. This project is written on top of amqp-connection-manager.

NOTE

Version 3 is a major and breaking change from Version 2. Please use appropriate version for your use.

Features

  • Automatically reconnect when your amqplib broker dies in a fire.
  • Round-robin connections between multiple brokers in a cluster.
  • If messages are sent while the broker is unavailable, queues messages in memory until we reconnect.
  • Very un-opinionated library - a thin wrapper around amqplib.

Configuration

{
    host: process.env.PUBSUB_RABBITMQ_SERVICE_HOST,
    port: process.env.PUBSUB_RABBITMQ_SERVICE_PORT_AMQP || 5672,
    username: process.env.RABBITMQ_USERNAME,
    password: process.env.RABBITMQ_PASSWORD,
    prefetch: process.env.PREFETCH_JOBS || 2,
    vhost: process.env.VHOST || '/',
    heartbeatInterval: process.env.HEARTBEAT || 5,
    reconnectTime: process.env.RECONNECT_TIME || 10,
    protocol: process.env.RABBITMQ_PROTOCOL || 'amqp',
    defaultQueueFeatures: { durable: true },
    options: {
      // options.findServers(callback) is a function which returns one or more servers to connect to. This should return either a single URL or an array of URLs. This is handy when you're using a service discovery mechanism such as Consul or etcd. Instead of taking a callback, this can also return a Promise. Note that if this is supplied, then urls is ignored.
      findServers,
      // options.connectionOptions is passed as options to the amqplib connect method.
      connectionOptions
    }
}

Usage

Using yarn: yarn add [email protected] OR Using npm: npm install [email protected]

import RabbitMQClient from 'node-rabitmq-client';
// (OR)
const RabbitMQClient = require('node-rabbitmq-client');

// instantiate a client object
const client = new RabbitMQClient(config);

/* to publish a message */
// `data` is JS object
client.publish({ queue: { name: 'some name' } }, data);

/* to consume from a queue */
client.consume({ queue: { name: 'some name' } }, promiseHandler);

/* to purge a queue */
client.purge({ queue: { name: 'some name' } });

/* to ack all messages */
client.ackAll();

Please read this for implementing consume

  • promiseHandler for consume should always return a resolved Promise even if some operations on the received message fails.
  • When returning a resolved Promise, parameters need not be passed to it.If passed, these are simply ignored.
  • Best practice is to implement a catch handler for the promiseHandler and push to some other queue and return a resolved Promise from there.
  • If parsing the JSON message fails while consuming, a rejected promise is thrown and needs to be handled appropriately (This is optional. Whether or not queue is provided, the error will be logged);
  • promiseHandler gets the message and the options that were passed to consume intially
/**
 *
  options is the object which is passed to consume at the time of initialization
  {
    queue: {
      name: 'some-queue-name',
      messagePriority: message priority (1-10), // set if the queue is a priority queue. It is optional
      options: {
        arguments: {
          'x-max-priority': queue priority (1- 10) // set to make the queue a priority queue. It is optional
        }
      }
    }
  }
 */
promiseFunction(message, options)
  .then(data => {
    /* once processing the message is successful, return resolved promise */
    /* if status queue is provided and success should be recorded */
    if (statusQueue && recordSuccess) {
      client.publish(statusQueue, {
        status: 'success',
        queueName,
        message: data
      });
    }

    /* this is needed to ack to the channel regarding this message */
    return Promise.resolve();
  })
  .catch(error => {
    if (statusQueue && recordError) {
      /* if status queue is provided and failure should be recorded */
      client.publish(statusQueue, {
        status: 'error',
        queueName,
        error,
        message
      }).then(() => channel.ack(msg));
    }
    logger.log('error', {
      note: `Error while processing the message from ${queueName}`,
      error: error
    });

    /* return resolved Promise from here */
    return Promise.resolve();
  });