# Message Broker using Rabbit-mq

*Originally published on January 20, 2024 on* [*my previous blog*](https://somesh-banerjee.github.io/blogs/blog/13)*.*

---

In the world of distributed systems, communication between services becomes a crucial aspect of building scalable and resilient applications. Message brokers play a pivotal role in facilitating this communication by enabling asynchronous messaging between various components. In this blog, we'll delve into the fundamentals of message brokers and explore RabbitMQ, a popular open-source message broker.

## What is a Message Broker?

A message broker is an intermediary software component responsible for facilitating communication between different applications or services. It allows for the decoupling of components by enabling them to send and receive messages asynchronously.

### Key Concepts

1. **Producer**: A producer is a component that sends messages to a message broker. It can be a client application, a microservice, or any other software component.
2. **Consumer**: A consumer is a component that receives messages from a message broker. It can be a client application, a microservice, or any other software component.
3. **Message Queue**: A message queue is a temporary storage location used by a message broker to store messages until they are consumed by a consumer.
4. **Exchange**: A routing mechanism that determines how messages should be distributed to queues.
5. **Routing Key**: A routing key is a value used by an exchange to determine which message queues to push messages to. It is a property of a message that is set by the producer.

Now that we have a basic understanding of message brokers, let's explore RabbitMQ.

## What is RabbitMQ?

RabbitMQ is an open-source message broker that implements the Advanced Message Queuing Protocol (AMQP). It is written in Erlang and is cross-platform. It is one of the most popular message brokers and is widely used in microservice architectures to facilitate communication between services.

### Key Features

1. **Message Durability**: RabbitMQ ensures that messages are not lost even in the case of a broker failure.
2. **Flexible Routing**: Messages can be directed to specific queues based on routing rules.
3. **High Availability**: Clustering and mirrored queues provide fault tolerance and high availability.
4. **Support for Multiple Protocols**: Apart from AMQP, RabbitMQ supports other protocols like MQTT.

### Setting up RabbitMQ

We will use Docker to set up RabbitMQ. If you don't have Docker installed, you can follow the instructions [here](https://docs.docker.com/get-docker/) to install it.

Once Docker is installed, we can pull the RabbitMQ image from Docker Hub and run it using the following commands:

```bash
docker run -d --hostname my-rabbit --name rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:3-management
```

## Hello World

Now that we have RabbitMQ up and running, let's write a simple Hello World program to get a feel for how RabbitMQ works.

We will have two components in our Hello World program: a producer and a consumer. The producer will send a message to a queue and the consumer will receive the message from the queue and print it to the console.

### Producer

```python
# pika is a Python client library for RabbitMQ
import pika

# Establish a connection to RabbitMQ
credentials = pika.PlainCredentials('user', 'password')
parameters = pika.ConnectionParameters(host='localhost', port=5672, credentials=credentials)
connection = pika.BlockingConnection(parameters)

# Create a channel
channel = connection.channel()

# Create a queue named 'hello'
channel.queue_declare(queue='hello')

# Publish a message to the queue
channel.basic_publish(exchange='', routing_key='hello', body='Hello, World!')
print(" [x] Sent 'Hello, World!'")

# Close the connection
connection.close()
```

### Consumer

```python
import pika, sys, os, time

# Configure connection parameters for RabbitMQ
credentials = pika.PlainCredentials('user', 'password')
parameters = pika.ConnectionParameters(host='localhost', port=5672, credentials=credentials)

def consume():
    # Define a callback function to print the message to the console when a message is received
    def callback(ch, method, properties, body):
        time.sleep(5)
        print(" [x] Received %r" % body)

    try:
        # Establish a connection to RabbitMQ
        connection = pika.BlockingConnection(parameters)

        # Create a channel
        channel = connection.channel()

        # Create a queue named 'hello'
        channel.queue_declare(queue='hello')

        # Consume messages from the queue using the callback function
        channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
        print(' [*] Waiting for messages. To exit press CTRL+C')
        channel.start_consuming()
    except Exception as e:
        print(e)
        return 'Queue not found'

if __name__ == '__main__':
    try:
        consume()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)
```

### Running the Program

We can run the producer and consumer in separate terminals. The producer will send a message to the queue and the consumer will receive the message from the queue and print it to the console.

```bash
python producer.py
python consumer.py
```

This is a simple example of how RabbitMQ works.
In real-world applications, we will have multiple producers and consumers. We will also have multiple queues and exchanges.
I have also created a repository which has a django application with exposed APIs to add message to queue and consumer script will consume the message from the queue. You can find the repository [here](https://github.com/somesh-banerjee/message-brokers-tutorial/tree/main/rabbit-mq)

