Introduction to RabbitMQ for Python Developers

what is going on guys welcome to the channel and today we are going to talk about rabbit mq and rabbit mk is one of the most popular message brokers and i found that together with python people usually use rabbitmq over other message brokers like for example kafka even though kafka is quite popular as a message broker if you are a python developer i would recommend you learn rabbitmq because if you're going to build applications using python most likely you're going to use rabbitmq as a message broker so in this video we are going to learn the fundamentals of rabbitmq and how to interact with it using python and pico library before i started i wanted to say that i will not go into the details of why rabbitmq is important and why you need to use it but if you're curious i have another video on that topic link will be in the description so with that said let's dive in rabbit mq is a message broker that implements different protocols but most importantly it implements the mqp advanced message queue protocol and this protocol was designed for systems to exchange messages using producers brokers and consumers producer believe it or not but it produces messages consumer consumes them and in between we have a broker that receives messages from the producer and sends them to consumers okay now let's take a look at how the broker works the broker has three components and the first component is exchange this component receives messages from the producer and then routes these messages to the queue also when we create an exchange we specify an exchange type there are different exchange types and we will talk about every one of them a little bit later but for now let's move on to the second component and the second component is q q is a data structure on disk or in memory that stores messages and the last component is binding binding is a connection between an exchange and a queue which tells an exchange what messages should be delivered to what queues now let's talk about different exchange types all of these components that i was trying to explain to you it's quite difficult to understand them without looking at any examples but right now you'll see all of this in practice and if you didn't understand something it should become much more clear now so the first exchange type is direct and an exchange with this type sends messages to the queue where routing key equals to binding key and if you look at the picture we have one exchange and three cues and the first queue is bound to an exchange with binding key a the second with binding key b and the last one with binding key is c and when producer sends a message to an exchange it specifies a routing key and the routing key equals to b and we only have one cue with the same key and it's the second key so the second queue receives this message another exchange type is topic exchanged with this type sends messages to cues where routing key partially matched to binding key so as you can see we have three different binding keys here and if you are familiar with regular expressions the syntax of binding key is quite similar for example the first binary key is order dot star and it means that if routing key starts from order dot the message will go to this queue another example is the last binding key star dot notify and it means that if routing key ends with that notify the message will go to this queue only the first and the last queue received the message that producer sent the second kid didn't receive the message because the keys didn't match another exchange type is fan out exchange with this type sends messages to all the cues that it knows about so it doesn't matter what routing key we have it doesn't matter what binding keys we have if the queue is bound to an exchange it will receive a message anyway and the last exchange type is header exchange with this type allows you to route messages based on header values instead of routing keys i'll not go into the details here since it works pretty much the same as previous exchange types now before we dive into python and pico library i wanted to mention the last thing about chebytonq and is the fact that rabbitmq has nameless exchange by default and this exchange compares routing key to queue name instead of binding key so if you publish a message to this nameless exchange with routing key equals to order this exchange will route this message to the queue with cue name equals to order so yeah that's everything i wanted to say about chebytonq now you should have a clear understanding of how rabbitmq works i hope now let's talk about how to create a simple python program it will help us to better understand producer broker and consumer flow in our program we will use pico library which is a rabbitmq client library for python and this library is not built into python so let's install it right now also i assume that you have already installed rabbitmq on your machine i'm not going to show you how to do that because there are a lot of tutorials like that online but if if you didn't do that i would suggest you run it in darker if you know how to use docker personally i don't like to install things like rabbit mq radius postgresql all of this stuff i prefer to just create a docker container and don't think about it so that's what we are going to do now let's run rabbitmq in docker container that's what you need to type in order to run rabbitmq now let's start writing some scripts we'll have three scripts in total the first one will be for publishing messages and the and two other scripts will be for consuming so let's start from publishing in this script what we do first is we connect to rabbitmq then we declare and exchange and the name of the exchange will be order and an exchange type will be direct then we let's say have some information about the order and we publish two messages both of these messages will go to the same exchange to order exchange but the first message will have routine key order dot notify and the the information that we will send in this message will be just user email and the second message will have routing key order that report and we will send all of the information that we have about order in this message so let's try to run this script so what we do is publish dot python publish that by now when we send two messages to rabbitmq let's open management in the browser so we have this url localhost15672 and we can type username guest and password guest and click on log in and if we open exchanges we will see that we have order exchange with type direct even though we send two messages one with routing key order that notify and the second one with router and key order that report this message just went nowhere because we don't have any queues that are bound to our exchange so let's create a consumer that will consume notify messages so we have this notify.pi file the same as in publish.pi file first of all what we do is we connect to rabbitmq however then we declare in uq and the queue will have a name order notified then we create a binding between our exchange with name order and our new queue and the binding key will be order that notify and then we are trying to consume messages so we call this basic consume method this method takes two parameters and it's a callback and a queue name and this callback is basically our consumer it will be called when the you when it receives a new message and first three lines are quite easy to understand we just get a payload and print two messages probably the most confusing line is the last one when we call basic acq method and basic acq is basic acknowledgement so we are basically trying to send an acknowledgement to rabbitmq and we are trying to say that the message was successfully received and processed and rabbitmq is free to delete this message this acknowledgement is useful when let's say our consumer receives a message but it dies for some reason in that case the message will not disappear from a queue because our consumer wasn't able to process the message and it didn't send an acknowledgement to rabbitmq so it didn't tell rapidmq that this message can be deleted now let's try to run these two scripts that we have published that buy and notify that by and let's see first of all let's run notify.pyscript so this script is waiting for notify messages let's publish some messages [Applause] as you can see it's working now let's move on to the second script and this script is going to consume report messages so our report.pi file is almost the same as notify.file we also declare in uq the name of the queue is order report and we also create a binding between a queue and exchange order the binding key is order that report now and we also have a callback function that processes our message and it sends an acknowledgement at the end let's try to run this script and as you can see the script is working and we were able to consume all our messages notify message and report message okay that's it for today guys if you are still watching this video i appreciate it so much and if you like the video give it a thumbs up and if you're new to the channel my name is denis and this channel is all about myself web development and growing as a full stack python web developer if that appeals to you consider subscribing and if you'd like to connect with me even further you can follow me on twitter or on my instagram links will be in the description thanks for watching guys see you in the next one you

Loading