@jakguru/amqplib-oop
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

amqplib-oop

This is a simple wrapper around the amqplib library to make it easier to use in an object-oriented way. Available for use in both TypeScript and JavaScript (CommonJS).

For more information, view the documentation.

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

Package Sidebar

Install

npm i @jakguru/amqplib-oop

Weekly Downloads

120

Version

1.1.0

License

MIT

Unpacked Size

296 kB

Total Files

51

Last publish

Collaborators

  • jakguru