DEV Community

Cover image for RabbitMQ: Open Source Message Broker Service
Ajay Krupal K
Ajay Krupal K

Posted on

RabbitMQ: Open Source Message Broker Service

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

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

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 server and amqplib locally.

//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));
Enter fullscreen mode Exit fullscreen mode

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.

//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)
})
Enter fullscreen mode Exit fullscreen mode

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😅 for more

Top comments (0)