Simplifying RabbitMQ Integration with Node.js Application

Ahmed K.
3 min readMay 14, 2023

--

Photo by Pankaj Patel on Unsplash

RabbitMQ is an open-source message broker service which helps scaling applications by message queuing mechanism by implementing Advanced Message Queuing Protocol (AMQP).

RabbitMQ works by offering an interface, connecting message senders (Publishers) with receivers (Consumers) through an exchange (Broker) which distributes the data to relevant lists (Message Queues).

     APP          EXCHANGE         TASK LIST          WORKER
[DATA] -------> [DATA] ---> [D]+[D][D][D] ---> [DATA]
Publisher EXCHANGE Queue Consumer

You can use RabbitMQ for decoupling, load balancing, task queuing, implementing publish/subscriber model, etc.

If you’re looking to kick off your NodeJS application with RabbitMQ, you’ll be pleased to know that it’s a simple process. By following a few easy steps, you can have your application up and running with RabbitMQ in no time.

Kindly take note that the installation guidelines outlined in this manual are tailored for operating systems based on Debian 7 or later.

The Installation:

apt-get    update 
apt-get -y upgrade
  1. Enable RabbitMQ repo & update
echo "deb http://www.rabbitmq.com/debian/ testing main" >> /etc/apt/sources.list
curl http://www.rabbitmq.com/rabbitmq-signing-key-public.asc | sudo apt-key add -
apt-get update

2. Download & install RabbitMQ

sudo apt-get install rabbitmq-server

3. Enable RabbitMQ Management Console

sudo rabbitmq-plugins enable rabbitmq_management

Voila! With the console now activated, simply fire up your preferred web browser and cruise on over to http://localhost:15672/ to gain access.

RabbitMQ interface launched in a web browser

The Node side:

Install client library amqplib using

npm i amqplib

After successful installation connect to RabbitMQ server & create channel:

import { Channel } from "amqplib";
import amqp from "amqplib/callback_api";

let channel: Channel;

const AMQP_SERVER = "amqp://localhost";

const MY_QUEUE = "MY_QUEUE";

amqp.connect(AMQP_SERVER, function (err, conn) {
if (err) {
console.log("AMQP connection error", err);
return;
}

conn.createChannel(function (err, ch: Channel) {
if (err) {
console.log("AMQP channel error", err);
return;
}
ch.assertQueue(MY_QUEUE, { durable: false }); // <--- assert queue means declare queue idempotently

channel = ch;
});
});

With channel & queue created send message to queue

channel.sendToQueue(
MY_QUEUE,
Buffer.from(
JSON.stringify({ message: "First time with MQ, feeling nervous!" })
)
);

Now consume message in any of application connected to mq server

 channel.consume(
MY_QUEUE,
async function (msg) {
if (msg) {
const { message } = JSON.parse(
msg.content.toString()
);

console.log(message)

channel.ack(msg); // <-- don't forget to acknowledge message
}
},
{ noAck: false }
);

You can check queued message the console as:

rabbitmqadmin list queues

Congrats, you made it here & processed your first message through RabbitMQ.

That concludes my update for now. For more of my coding and tech endeavors, feel free to connect with me on LinkedIn or visit my Github profile and stay tuned for future updates.

--

--

Ahmed K.

Software Engineer; Coding while time-traveling through World History!