RabbitMQ: Open Source Message Broker Service
Tags: webdev, javascript, tutorial, learning
Published: 2024-07-12 14:46:19

RabbitMQ is an Open Source message broker service aiding systems with microservice architecture. For example, let's say you want to upload a video to YouTube, behind the scenes, there might be one service to upload the video, one to notify people who are subscribed to the author, and so on. The upload service appends “New video” events to a RabbitMQ stream. Multiple backend applications can subscribe to that stream and read new events independently of each other.

![RabbitMQ Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwfzy6hqowu6d2zhwxk7.png)

With the same example of a video streaming service, the producer is when the video is uploaded to the platform which can happen through an API. The API then produces a message with the required data which publishes it to an exchange. The exchange then routes it to one or more queues which are linked to the exchange with a routing and binding key. The message then waits in the queue until it is consumed by the consumer which in this case is the uploads service.

![Exchange](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wssz367odw3wxfgb23xc.png)

The Exchange can behave in one of these three ways:
- Sending the message directly to a specific queue or
- Multiple queues with a shared pattern using topics or
- To all the available queues

A basic example of sending a message to RabbitMQ locally. The below code is to send a message to the queue. Pre-requisites for this is to install [RabbitMQ](https://www.rabbitmq.com/docs/download) server and [amqplib](https://www.npmjs.com/package/amqplib) locally.

```js
//library that implements messaging protocol
import { connect} from 'amqplib'

// connect to rabbitmq locally
const connection = await connect('amqp://localhost');
const channel = await connection.createChannel();

const queue = 'test';
const message = 'hello world'

// queue can be durable either stored in RAM or memory
await channel.assertQueue(queue, {durable: false});

// message is produced
channel.sendToQueue(queue, Buffer.from(message));
```
Now that the message is sent to the queue, we need another file to receive the message, which can also be a server subscribed to the same queue and RabbitMQ client.

```js
//library that implements messaging protocol
import { connect} from 'amqplib'

// connect to rabbitmq
const connection = await connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'test';
await channel.assertQueue(queue, {durable: false});

//consume the message
channel.consume(queue, (message) => {
    const msg = {...message}
    console.log("Received message: " + msg.content)
})
```

When both files are run simultaneously, the message should be received in the terminal of the second file as "hello world".

Alternatively, you can use CloudAMQP to create a RabbitMQ instance on the cloud and replace the localhost connection string with the one on the cloud. If you need a more detailed RabbitMQ blog do let me know in the comments. 

Until then, follow me on [twitter](https://x.com/ajaykrupalk)😅 for more