RabbitMQ Tutotial for Python (using Pika)

What is RabbitMQ?

RabbitMQ is an open-source message broker software that originally implemented the Advanced Message Queuing Protocol (AMQP). It facilitates the efficient delivery of messages in a distributed system, enabling applications to communicate with each other through message passing.

RabbitMQ is used for its reliability, scalability, and flexibility. It's particularly useful in decoupling systems or components, allowing for asynchronous processing, which can improve the scalability and efficiency of applications.

Use Cases:

  • Asynchronous Task Processing: Offloading tasks that can be performed later, like sending emails or processing images.
  • Inter-Service Communication: Enabling microservices to communicate in a decoupled manner.
  • Distributed Computing: Coordinating and distributing workloads across various computing nodes.
Building a webhook service? With Svix, you can build a secure, reliable, and scalable webhook platform in minutes. Give it a try!

Tutorials

We'll show how to create a producer (a.k.a. sender) for sending messages to a RabbitMQ queue as well as a consumer (a.k.a. listener or receiver) for receiving messages from the queue.

Prerequisites

  • Python environment
  • RabbitMQ server (Install guide: RabbitMQ Installation)
  • pika library (Install using pip install pika)

Creating a Producer (a.k.a. Sender)

Step 1: Import Pika Library

import pika

Step 2: Establish Connection

Connect to a RabbitMQ server:

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

Step 3: Declare a Queue

Declare a queue to send messages:

channel.queue_declare(queue='hello')

Step 4: Send a Message

Publish a message to the queue:

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

Creating a Consumer (a.k.a. Listener/Receiver)

Step 1: Import Pika and Set Up Connection

Similar to the producer, import pika and set up the connection:

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

Step 2: Declare the Same Queue

Make sure to declare the same queue as the producer:

channel.queue_declare(queue='hello')

Step 3: Define a Callback Function

Define the function to be called when a message is received:

def callback(ch, method, properties, body):
    print(f" [x] Received {body}")

Step 4: Set Up Queue Consumption

Tell RabbitMQ that this callback function should receive messages from the queue:

channel.basic_consume(queue='hello',
                      on_message_callback=callback,
                      auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

Conclusion

With RabbitMQ and Python, you can set up robust and scalable message-based communication for your applications. This guide provides the basic steps to create a producer and consumer in Python, enabling you to implement efficient message handling in your system.