Simplify RabbitMQ Integration in Node.js with @kazinaimul/rabbitmq

kazinaimul

Kazi Naimul Hoque

Posted on December 23, 2023

Simplify RabbitMQ Integration in Node.js with @kazinaimul/rabbitmq

Image description

Are you tired of navigating the complexities of RabbitMQ integration in your Node.js projects? Well, fret no more! I’m excited to introduce the @kazinaimul/rabbitmq package — a straightforward solution to streamline your RabbitMQ integration seamlessly.

Installation Made Easy

Let’s kick things off with a swift installation. Open your terminal and run:

yarn add @kazinaimul/rabbitmq

or

npm install @kazinaimul/rabbitmq
Enter fullscreen mode Exit fullscreen mode

Getting Started: Establishing Connection

In your Express app’s app.js file, pave the way for RabbitMQ integration with this simple code snippet:

import { RabbitMQConnection } from '@kazinaimul/rabbitmq';
RabbitMQConnection.connect('RABBITMQ_CONNECTION_URL');
Enter fullscreen mode Exit fullscreen mode

Swap out RABBITMQ_CONNECTION_URL with your RabbitMQ server connection URL, neatly formatted as _**amqp://username:password@localhost:5672**_.

Effortless Message Publishing

Publishing messages becomes a breeze with the creation of a Publisher class. Check out this snippet:

import { Publisher } from '@kazinaimul/rabbitmq';
export class PublishMessage extends Publisher {
 constructor() {
   const queueName = 'queue-name';
   super(queueName);
 }
async publish<MessageType>(message: MessageType): Promise<void> {
   try {
     const customOptions = {
     exchange: `Exchange_name`,
     routingKey: queue,
     delay: 0,
     exchangeType: "direct",
     headers: {},
     }; // Custom optional values, overwriting default options
     await this.rabbitMQClient.publish(this.queueName, message, customOptions);
   } catch (error) {
     console.error('Error publishing messages:', error);
   }
 }
}
Enter fullscreen mode Exit fullscreen mode

By default the option for publish a message is followed which can be replaced:

const defaultOptions = {
exchange:
Exchange_${queue},
routingKey: queue,
delay: 0,
exchangeType: "direct",
headers: {},
};

Seamless Message Consumption

Creating a Consumer class ensures smooth message consumption. Take a look:

import { Consumer } from '@kazinaimul/rabbitmq';
export class MyConsumer extends Consumer {
 constructor() {
   const queueName = 'queue-name';
   const options = {
     retry: true, // If true, messages will be queued again in case of an error (default is true)
     retry_count: 3, // Maximum retry count, beyond which the message will be moved to an error queue
     retry_delay: 0 // Delay in milliseconds for retries
   };
   super(queueName, options); // Options are an optional field
 }
async execute<T extends object>(message: T): Promise<void> {
 // Implement your own logic to handle the consumed message
 }
}
Enter fullscreen mode Exit fullscreen mode

Register and Consume

Don’t forget to register your consumers in a file (let’s call it consumerRegister.ts):

// Import the consumer classes
import { MyConsumer } from './MyConsumer';
export const consumerRegister: any[] = [
  new MyConsumer()
  // Push all other Consumer classees here
];
In your application, consume messages effortlessly by iterating through the registered consumers:

import { consumerRegister } from './consumerRegister';
// Consume messages from registered consumers
for (const consumer of consumerRegister) {
 consumer.consume();
}
Enter fullscreen mode Exit fullscreen mode

Replace ’queue-name’ with your chosen queue name.

Feel free to explore, contribute, and simplify your RabbitMQ integration. Happy coding! 🚀🐇

Check out the package on npm!

💖 💪 🙅 🚩
kazinaimul
Kazi Naimul Hoque

Posted on December 23, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related