hi guys welcome back to another video on rabbitmq in the last video we looked at some of the basics of rabbitmq and its common terminology in this video we're going to write our very first simple program using rabbitmq in this video we'll be using python but if you prefer to use net and c-sharp please skip ahead to the next video as the content will be the same before we start writing our code we'll give a brief introduction on what we are trying to achieve all we are trying to achieve is to publish a message into our rabbitmq broker from our producer and then consume that message from a consumer so we will have a producer and a consumer so the consumer in this video will consume his messages from a queue so like we discussed in the previous video the queue is like the post box for the consumer so here's our queue and the consumer will simply listen to messages from this queue we have to give the queue a name and in this example we'll call our cue letterbox in rabbitmq we can never publish a message directly to a queue it has to go through an exchange in this very simple example the exchange is not that important so we can just use the default exchange which is represented by a blank string so the producer will push a message onto the default exchange default exchange will then push that message into our letterbox queue and finally as we've seen the consumer will consume the message from the letterbox queue so now we're going to create our first rabbitmq producer and consumer we're going to use python for this and we're going to create two separate python files producer.pi for our producer and consumer.pi for our consumer we're going to need to install a package from pip to do this as well the package is called pica and it's a rabbitmq client library so we can use this library to send or receive messages from our message broker i'm using visual studio code for this example but you can use any ide or text editor you're comfortable with to run the code so first thing i'm going to do is i'm going to open a new terminal and i got to install pica so i'm going to type pip install pica and that should add the pica package i need in order to run my code next it's going to create a new file so click new file i'm going to call it producer.pi this is where i'm going to write my producer code so the first thing i want to do is i want to import the pica library so simply just type import pica next i need to create a connection to my locally running rabbitmq message broker to do this i want to type dot connection parameters i'm just running on localhost but if you were running on a real server you could enter the proper ip address or dns name here and i'm going to save that in a variable called connection parameters so next i'm going to use these connection parameters to create a connection to my rabbitmq broker i'm going to save that connection itself in a variable called connection and i'm going to say pica dot blocking connection i'm going to pass in my connection parameters so now we have the connection we want to create a channel remember we said that you don't interact directly with connections you interact with channels and a connection kind of many different channels so again we want to create a variable to store our channel and we want to say connection dot channel this will create a channel for us we don't need to give it a channel number we'll just settle for the default so now we've got our connection and our channel what we want to do is we want to see how can we use this to send a message onto the broker so the first thing we need to do is we need to declare a queue so we can use channel.declare queue or sorry cue declare and then we need to give it a cue name so i'm just going to call the cue letterbox so now that our cues declare we want to publish a message to us now we don't publish a message directly to a queue ever everything needs to go through an exchange but in this case we're just going to use the default exchange so let's first define the message we want to send so say we want to just send hello this is my first message and then we want to send this so we want to send it on the channel not the connection and we just want to use the basic publish method so basic publish and we have to give it the default exchange so that's just an empty string because we have to publish to exchange but in this case we're just going to use the default one the rooting key should be the name of the queue that we want to publish to so in our case we called the cue letterbox so it has to be letterbox and then finally we want to give it the body of the message which is the message we want to send itself so that's in our message variable and we'll just print off that we've sent the message so we'll use f strings for this we'll say send message and then we'll say the message that we sent and finally the last thing we want to do in our producer is just close the connection so we should be tidying stuff up after ourselves and not leaving connections hanging around so that should do it for our producer you can see all the code here and i'll leave a link to the code on my github in the description so if anybody wants to clone the code for there or check it out there please feel free to do so so next we're going to create our consumer so the consumer is what we're going to use to actually consume the messages off the broker so the producer will push them on and the consumer will pull them off so let's create a new file and call it consumer.pi and again the first thing we want to do is import the pica library and again the code here will be quite similar in order to establish the connection and the channel so we can just copy that in from our producer so again we're connecting to localhost and we're using the blocking collection and creating a channel so we can just copy that in and also we're going to declare the same cue in the consumer as the producer it doesn't matter if the queue is declared twice as the rabbit mq broker will know just to declare the queue once it's what's known as an item potent operation so we can declare it in both places and wherever the code executes first will actually declare the queue the other place it will just be ignored so once we have the queue declared on the channel we want to use the basic consume method in order to consume off the queue so this method takes a couple of different parameters the first one is the queue we want to consume off and in our case again it's the one we defined earlier called letterbox there's a couple of other problems we need to specify auto act which is basically saying do we want to automatically acknowledge the messages we'll set that to true for now and finally we need to give it a call back so we need to give it something to do when it receives a message so that is in the on message callback parameter and we need to give this a call back so we'll create a method up here and we just call it on message received and that takes the channel the method properties and the body itself and for now we'll just print that we've received a message here so say receive new message and then we'll also print out the body of the message so we want to pass this method here as the callback into our basic consume so now that we've set that up we've created the channel we've created the connection to clear the queue called basic consume we should now start consuming messages so we'll just print off here started consuming and then finally we need to call channel dot start consuming so this is what we'll block and consume the messages and every time it receives a message it will call this call back here okay so let's run this so we're going to open a new bash terminal window and we're just going to run our consumer first so we'll just type python consumer dot pi and we can see our consumer has started and it says start consuming if we have a quick look in the rabbit mq management ui we can see some interesting things we can see we have a connection here with the protocol amqp091 with one channel we can also see cues that our letterbox queue has been created so let's go back to visual studio code let's open another terminal window and let's run our producer so let's type python producer.pie and we can see we sent a message hello this is my first message and if we look at our other terminal window we can see started consuming received new message hello this is my first message and if we run the producer again we should receive a second message so again we'll go do that and look back at our consumer again we've received another message hello this is my first message even though we know it's the second message so let's just stop the consumer for a second so the consumers stop now and let's run the producer again without the consumer running so we've sent another message but there's no consumer running to consume it if we look in the rabbitmq management client for our letterbox we can see there's actually a message sitting in there that hasn't been consumed yet so this is the message we just sent that hasn't been consumed so if we go back and again start our consumer it receives a message as soon as it connects because that was the message sitting in the queue and if we just look back that message now in the ui is gone so that was a very quick first program in rabbitmq on how to create a producer and a consumer in python using the pica client library so thanks for watching this video guys in the next video we're going to go through the same code but using.net and c-sharp so as i said feel free to skip that video if you've already watched this one and are happy to use python in the video after that we're going to take a slight detour to look at the details of amqp which is the underlying protocol that rabbitmq uses if you enjoyed the video and want more rabbitmq content don't forget to like the video and subscribe to the channel