1.0.3 • Published 5 years ago

@thealmondtree/rabbitmq v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
5 years ago

RabbitMQ Connection Wrapper

Package written in Typescript that helps connecting to a RabbitMQ queue service via the amqplib npm package.

Install

npm i @thealmondtree/rabbitmq

Exports

This library exposes three functions to handle messages themselves and two functions to setup the connection.

  • Set up:

    • setupChannel: Function that receives a config object. With the config object data or the default options stablishes a connection to a RabbitMQ server, creates a channel and stores the resulting channel and the given queue in local variables for the other methods to use.
    • isChannelReady: Function that returns a boolean determining wheter or not a channel reference is already stored in the local _channel var for the functionality to work with the default values
  • Functionality:

    • send: Function that takes in a string message (or an strigified object) and a channel and a queue which have the localy stored values by setupChannel, checks for the validity of the channel and then sends the message to the queue.
    • receive: Function that accepts a channel and a queue as parameters but can use the default values instead, validates the channel and fetches a single message from the queue. Does not execute acknowlegment.
    • clear: Function that, given a message from a previous fetch from the queue, and a channel that defaults to the local one, executes an acknowledgement call to the queue for the message to be removed.

Setup Config Options

  • queue: string => No default value, target queue to use.
  • hostname: string => Defaults to 'localhost'
  • port: number => Defaults to 5672
  • username: string => Defaults to 'guest'
  • password: string => Defaults to 'guest'
  • frameMax: number => Defaults to 0. The size in bytes of the maximum frame allowed over the connection. 0 means no limit (but since frames have a size field which is an unsigned 32 bit integer, it’s perforce 2^32 - 1); I default it to 0x1000, i.e. 4kb, which is the allowed minimum, will fit many purposes, and not chug through Node.JS’s buffer pooling. reference
  • heartbeat: number => Defaults to 0. The period of the connection heartbeat, in seconds. To learn more about the heartbeat option click here

Example

const { setupChannel, isChannelReady, send, receive, clear } = require('@thealmondtree/rabbitmq')

const config = {
  queue: 'my_awesone_queue',
  hostname: 'localhost',
  port: 5672,
  username: 'guest',
  password: 'guest',
  frameMax: 0,
  heartbeat: 0
}

async function send() {
  if (!isChannelReady()) await setupChannel(config)

  send(JSON.stringify({ message: 'hello' }))
}

async function receive() {
  if (!isChannelReady()) await setupChannel(config)

  const message = await receive()
  /*
    The actual content of the message is in the content property of the
    message, stored in a Buffer. The JSON.parse() method is to be used if the message is
    expected to be an object
  */
  const content = JSON.parse(message.content.toString())

  // Do stuff with the message

  // To clear the message from the queue you should send the original message object
  clear(message)
}
1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago