Asynchronous Communications Using RabbitMQ Message Broker


RabbitMQ is a message broker. It accepts the messages from the sender, stores these messages in the queue, and delivers them to the receiver.

A message broker is just like a mediator between the sender and receiver. The purpose of a broker is to take incoming messages from applications and perform some action on them and route these messages to the correct receiver.

Inside RabbitMQ, the messages are stored in a queue that works on the FIFO principle, that means the messages that arrived first will be delivered first. These message queues provide an asynchronous communications protocol, meaning that the sender and receiver of the message do not need to interact with the message queue at the same time. Messages placed onto the queue are stored until the recipient retrieves them.

Before using RabbitMQ in our project, we need to install the RabbitMQ Server on our computer.

Note - To install RabbitMQ Server, we need to install Erlang OTP. So, here are the installation steps to set up your system.

Installation Guide

The first step is to download Erlang OTP from the given link.

After downloading it, download the RabbitMQ Server from the following link.

Now, the first step is to install the Erlang OTP. So, double click on the EXE file and install it.

Just click to "Next" and "Next" until finishing the installation.


After Installation completes, install the RabbitMQ Server as follows.

Allow access for the Firewall, as shown below.

After allowing the Firewall, just click on Finish button to finish the installation process.


Now, our working environment is ready, let's check the services for checking the RabbitMQ Service.

Now, we need some set up to check and manage the queues.

For this, we need to enable the RabbitMQ Web Management. So, for enabling this, go to the Start and search for sbin command prompt.

Now, enter the following command to enable the Web Management.
  1. rabbitmq-plugins enable rabbitmq_management


The rabbitmq_management plugin is a combination of the following plugins. All of the following plugins will be enabled when you execute the above command,

  • mochiweb
  • webmachine
  • rabbitmq_web_dispatch
  • amqp_client
  • rabbitmq_management_agent
  • rabbitmq_management 
After this, just type http://localhost:15672/. It will open the following RabbitMQ web page.

Bydefault RabbitMQ runs on 15672 port No.

Now, the default UserId and password for this is "guest". After logging in with the Userid and Password, it will take you to the portal where you can check your Queues, create Queues, delete Queues etc.

Now, let's start Visual Studio, and create a Windows Application project. Now, go to NuGet Package console and install the RabbitMQ Client.

And, here I have designed my Form as below.

Now, Under Submit button click, write the following code.
  1. private void button1_Click(object sender, EventArgs e) {  
  2.     var factory = new ConnectionFactory() {  
  3.         HostName = "localhost"  
  4.     };  
  5.     using(var connection = factory.CreateConnection())  
  6.     using(var channel = connection.CreateModel()) {  
  7.         channel.QueueDeclare(queue: "MyQueue",  
  8.             durable: false,  
  9.             exclusive: false,  
  10.             autoDelete: false,  
  11.             arguments: null);  
  12.         string message = textBox1.Text;  
  13.         var body = Encoding.UTF8.GetBytes(message);  
  14.         channel.BasicPublish(exchange: "",  
  15.             routingKey: "MyQueue",  
  16.             basicProperties: null,  
  17.             body: body);  
  18.     }  
  19.     MessageBox.Show("Message sent successfully.");  
  20. }  
  21. }  
Note - To work with RabbitMQ, we need the following Namespace.
  1. using RabbitMQ.Client;  
Code Explanation

First, we are creating a connection to the RabbitMQ Server. Here, we are connecting to a broker on the local machine; so we put the localhost.

If we wanted to connect to a broker on a different machine, we'd simply specify its name or IP Address here. So mainly, for the connection, it will take 3 parameters.
  1. Server Address
  2. UserName
  3. Password
  4. Creating a channel for communication between UI and Server.
  5. Creating a queue for storing messages.

After that, we are simply collecting the messages from UI and sending to the Server. Now, just try to send some messages and let's check the queue.

Here, if we want to check the MyQueue by double click on it, it will take you to a page where you can see your messages in the queue.

Here is the overview of the MyQueue.

So far, we have successfully sent the message from sender to RabbitMQ Server. In our next part, we will learn how to retrieve these messages by creating a client.

I hope you understand the basics of RabbitMQ message broker. If you have any doubt, you can give your feedback so that we can try to explain it to you more clearly.

Similar Articles