Create your first messaging application with RabbitMQ

Reading Time: 3 minutes

What is RabbitMQ and Why we should use it?
We often get this question in our mind when we look to any project and see RabbitMQ in its technical stack. So, this blog will give you a brief introduction to RabbitMQ.

RabbitMQ is a message-broker whose basic functionality is to accept and forward the message. It is an open-source enterprise messaging system modeled on the Advanced Message Queuing Protocol (AMQP) standard.

AMQP 0-9-1 (Advanced Message Queuing Protocol) is a messaging protocol that enables conforming client applications to communicate with conforming messaging middleware brokers.

The transfer of messages between producer and consumer does not take place directly. Instead, we have an exchange and a queue in between them. Now, the question comes what is an exchange and what is a queue here for?

Basically, an exchange is a very simple thing. On one side, it receives messages from producers and the other side it pushes them to queues. The exchange is responsible for deciding what should happen to the message. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded? All these rules for message routing are defined by the exchange type.

A queue, on the other hand, is a buffer that stores messages. In the example below, we will be creating a basic RabbitMQ producer and consumer in python using Pika client. You can install it using the command

pip install pika

In producer.py, we will be sending Hi!! and the same message will be received by consumer.py

Here is the code for producer.py

import pika
# Establish a connection with RabbitMQ server 
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# Create a greeting queue to which the message will be delivered. If we send a message to non-existing location, RabbitMQ will just drop the message. 
channel.queue_declare(queue='greeting')

# Publish message with default exchange, routing key and message body 
channel.basic_publish(exchange='',
                      routing_key='hi',
                      body='Hi!')
print("message sent!!")

# Flush network buffers and close the connection
connection.close()

In the above code, we have used a default exchange which is a pre-declared direct exchange with no name, usually referred by the empty string “”. In this case, your message is delivered to the queue with a name equal to the routing key of the message. Every queue is automatically bound to the default exchange with a routing key which is the same as the queue name.

direct-exchange

There are other types of exchanges as well like topic, fanout, headers. Let us discuss the types of exchanges in brief:

Direct: It delivers messages to the queues based on the routing key.
Topic: It delivers messages to one or many queues based on matching between a message routing key and the pattern that was used to bind a queue to an exchange.
Fanout: It delivers messages to all of the queues, bounded with that exchange and the routing key is ignored.
Header: A headers exchange is designed for routing on multiple attributes that are expressed as message headers.

Now, its time for creating our consumer. Here is the code for consumer.py

import pika

# Establish a connection with RabbitMQ server.
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

# Create a greeting queue. Creating a queue is idempotent.
channel.queue_declare(queue='greeting')

# Subscribe to callback to print the response on the screen.
def callback(ch, method, properties, body):
    print("Received %r" % body)

# Tell RabbitMQ that the above callback should recieve messages from greeting queue
channel.basic_consume(callback,
                      queue='greeting',
                      no_ack=True)

print('Waiting for messages....')
channel.start_consuming()

To see the code in action, run on consumer.py on one terminal using the command

python consumer.py
# => Waiting for messages.... 
# => Received 'Hi!'

Also, run your producer.py on the other terminal.

python producer.py
# => message sent!!

You can see, we were able to send out the first message using RabbitMQ.

I hope, you have liked my blog. If you have any doubt or any suggestions to make please drop a comment. Thanks!


References:
RabbittMQ Official Doc

Knoldus-Scala-Spark-Services

 

 

Written by 

Vinisha Sharma is a software consultant having more than 6 months of experience. She thrives in a fast pace environment and loves exploring new technologies. She has experience with the languages such as C, C++, Java, Scala and is currently working on Java 8. Her hobbies include sketching and dancing. She believes Optimism and a learning attitude is the key to achieve success in your life

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!