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

@jakguru/amqplib-oop

v1.1.0

Published

An Object-Oriented Wrapper around amqplib

Downloads

355

Readme

amqplib-oop

This is a simple wrapper around the amqplib library to make it easier to use in an object-oriented way.

Doc Coverage Badge

Installation

npm install @jakguru/amqplib-oop

or

yarn add @jakguru/amqplib-oop

Usage

Import / Require the library

import { Connection } from '@jakguru/amqplib-oop'

or

import Connection from '@jakguru/amqplib-oop'

or

const { Connection } = require('@jakguru/amqplib-oop')

Create an instance of the Connection

const client = new Connection()

Get an instance of a Queue

const testQueue = client.getQueue('test-queue')
const testQueue2 = client.getQueue('test-queue-2')

Setup a listener for a queue

testQueue.listen((message, ack, nack) => {
  // Do something with the message
  // if successful: ack
  // if failed and you want to requeue: nack()
  // if failed but you don't want to requeue: nack(false)
})

Enqueue a message

await testQueue.enqueue(Buffer.from('Hello World!'))

Instrumentation

When debugging an issue, it is important to be able to see what is actually happening. In order to faciliate this, the library instrumentation hooks which can be set on both Connection and the Queue instances. This allows you to hook instrumentors like NewRelic into your connections and queues to get a deeper understanding of how your application is using RabbitMQ.

Here's an example of how to use the instrumentation hooks on the Connection instance. You can see that it implements all of the possible instrumentors available on the ConnectionInstrumentors interface.

import { Connection } from '@jakguru/amqplib-oop'
import newrelic from 'newrelic'
const config = {} // Your RabbitMQ config
const connection = new Connection(config, {
  assertQueue: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.assertQueue'),
  createChannel: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.createChannel'),
  eventEmitter: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.eventEmitter'),
  eventListener: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.eventListener'),
  getQueue: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.getQueue'),
  initialization: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.initialization'),
  shutdown: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.shutdown'),
})

Here's an example of how to use the instrumentation hooks on the Queue instance. You can see that it implements all of the possible instrumentors available on the QueueInstrumentors interface.

import { Connection } from '@jakguru/amqplib-oop'
import newrelic from 'newrelic'
const config = {} // Your RabbitMQ config
const connection = new Connection(config, {... your connection instrumentation hooks ...})
const queue = await connection.getQueue('your-queue-name-here', {...your queue options}, {
  preShutDown: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.preShutDown'),
  shutdown: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.shutdown'),
  check: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.check'),
  delete: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.delete'),
  purge: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.purge'),
  enqueue: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.enqueue'),
  ack: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.ack'),
  nack: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.nack'),
  get: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.get'),
  listen: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.listen'),
  pause: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.pause'),
  eventListener: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.eventListener'),
  eventEmitter: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.eventEmitter'),
  messageListener: newrelic.startBackgroundTransaction.bind(
    newrelic,
    'rabbitmq.messageListener'
  ),
  tick: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.tick'),
  consumer: newrelic.startBackgroundTransaction.bind(newrelic, 'rabbitmq.consumer'),
})

TODO Eventually

  • [ ] Add more test cases
  • [ ] Add more documentation & examples
  • [ ] Add "Exchange" functionality