1.1.0 • Published 3 months ago

@jakguru/amqplib-oop v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

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
1.1.0

3 months ago

1.0.19

6 months ago

1.0.18

6 months ago

1.0.17

6 months ago

1.0.16

6 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.0

10 months ago

0.10.3

10 months ago