RabbitMQ, A Message Broker for Python
AsiriNaidu Paidi
Software Engineer | Full-Stack Developer |Data Scientist | Machine Learning Enthusiast | Python Expert
RabbitMQ is one of the widely used message brokers. It accepts messages, stores and sends messages to destination. It has a support for connecting with multiple languages. Here, we are considering python language and its applications with RabbitMQ.
In general, RabbitMQ is used between a producer and consumer. A producer is a program which generates messages and pushes them to queue, on the other hand, consumer is also a program that will wait to receiving messages from queue. Many of the web and mobile technologies are using this service to exchange information between web client and the server. Before reaching message broker, other network and API calls will take the advantage of storing data to some repository. Big data and IOT applications use NoSQL databases.
There is no rule set for placement of producer and consumer -- both needs to be sit in the same machine. As per the application demands, these two entities can be placed remotely as well.
Sending
Before sending any messages, it is recommended that we must establish connection with the broker then only sender can able to push messages.
import pika
connection = pika.BlockingConnection( pika.ConnectionParameters( host='localhost' ) )
From the above code pika is a library to be used to establish connection between producer and consumer. In the connection string, we can clearly be able to understand that the message broker sits in localhost. If the broker presents in some other location, simply provide IP address for a successful connectivity. Before pushing a message on RabbitMQ, check whether the queue has been created or not. If not, create a queue using queue_declare.
Receiving
Same process as like sending side, first we need to connect to RabbitMQ server to receive, server can be ready to read content. Code for connection string to connect RabbitMQ is same as the previous one.
Before entering into further steps, make sure that the queue exists. If messaging queue doesn’t exists in the network, create it by using queue_declare property.
RabbitMQ Features:
1. Flexible for development: RabbitMQ is developers friendly queuing technique, used with multiple programming languages. It supports python, java, c#, Go etc.
2. Asynchronous messaging: Asynchronous communication will not happen in real-time, it is most important to note that this technique is widely used in cloud based applications. In such applications, RabbitMQ will help. It provides acknowledgements for asynchronous messaging.
3. Monitoring and Management: There are different tools available for managing and monitoring RabbitMQ such as HTTP-API, command line and GUI Based tools. These tools show the traffic of each queue from the RabbitMQ server. By the help of GUI tools, we can clearly see the inflow and out go traffic.
4. Enterprise and cloud ready: Message brokers provides the pluggable services. Due to this advantage over cloud it can be placed easily.
Do let me know your thoughts writing to me at [email protected] or use the comments section below.
MSc Master in Software Engineering | AWS | lang = [ Java, Kotlin, Python, Nodejs ] | Gen AI | Fintech | B2B | C2C
11 个月thanks for posting...
I cook food and data. | "Hello world" coder
7 年Well explained. But would like to know if writing messages in this queue is one-to-one approach or many-to-many approach. Suppose if we have multiple producers and consumers, then how will we be handling this situation ? Please share your valuable thoughts. :-)