Send and Receive Messages From RabbitMq with Pika and Python

Author:

Programming With Spirit

Keywords:

Python and Rabbit,Python and rabbitmq,Python Pika,Pika,Pika RabbitMq,Send with Pika to Rabbit,Receive from Rabbit With pika,Consume from Rabbit with Pika,Pika and Python,How to start with rabbit and python,how to start with pika,What is Pika,Send and Receive Messages From RabbitMq with Pika and Python,Demonstration on using Python client Pika to send and receive RabbitMq messages

Subtitles:
in this video I create two simple programs for publishing and consuming messages from rabbitmq with Python I will be using recommended like a client library for Python pecan and I have [Music] management UI open for a bit in queue there is no queues at this point and I already installed pika but this is document it can be used to install and great so once this done I'm doing this in Visual Studio code so the first program will be for sending messages to rabbit so the first thing we need just import become then we need to create connection channel so first we'll create parameters for our URL let's pick out your parameters and you specify the URL if it's a remote host you will specify a Pinterest have local host so you can just put here just local host but I'm just specifying specifying whole URL URL and this part is virtual host and if it's just default virtual host you can call this like this after that we use in blocking connection and passing our parameter and creating a channel on this connection : channel next step on the channel we call in queue declare and pass in the queue name I just call it test queue name then on the channel we call in bath basic publish method which accept exchange routing key message body and properties so I don't have any exchanges so I'm using here a little trick using just empty streets train for exchange it's because you have to publish messages to exchange and then it will route to the queue but to send to simplify example I will provide the ramp routing key here my cue name so it will go directly into cue a message body is just simple string and publish to the queue it can be object whatever but in my case it's just a simple string and here are some parameters content type in the liberal mode and after I'm done I want to close connection which will close channels and disconnect so if I run program and go to my cues I see the test queue with one message it wasn't here before and I see here some the moments and you can get message and this is play method body and now we'll create the simple program for receiving messages there is two ways the first one will be very simple for just consuming one message so we also need import picker and the first part is exactly the same just create connection great channel declare pew this is not necessary if you know exactly that the queue was created but it's a good practice just to create you because it will the queue will be created only if it doesn't exist it's basically create queue or choke and we call based basic get on the channel which just gets one message from the queue and towards method properties and body so then we check if method we will print whatever was received and send basic acknowledgment for this message advice with the print normal shootin so let's run this so here I gets ya the message is message body and let's go back to our UI and you see that there is no messages anymore and come back and I'll comment out this part where I'm using basic yet I'm allowed this and I have here another way of getting messages from the queue it's consuming messages so basically the program is running all the time and listening for messages instead of just getting one message it will be consuming messages so we leave the message appears in the queue is consumed appears of the queue is consumed so here we'll need specify call back with the channel method properties and body so we will here will be just printing body of the message and now we will call it basic consume versus basic ad which will be consuming messages and we need to pass queue name here and call back it's a function we defined and we will set out acknowledgment to troll with it automatically sent acknowledgement and we will print just some information waiting for messages to the press control-c and this method really calling the channel start consuming to start listening so that and [Music] I can start the problem so it's waited for messages open another terminal and I'll start my program for sending messages so and if I go back to my terminal for listen messages which will do but this you're saved message one and if I go again publish another message go here and I got another message or if I stop this program and let's send a couple messages three messages so we see three messages in the queue and go back run for some messages and it consumed all three messages from the key so I can stop it and this is this simple way for start working bees pick a client for Python RabbitMQ and thanks for watching and see you in my next video bye

Loading