Asynchronous Communications Using RabbitMQ Message Broker

Introduction

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 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, which 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 in the queue are stored until the recipient retrieves them.

RabbitMQ

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

Note. To install the 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.

Erlang OTP

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.

EXE file

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

Next

After Installation completes, install the RabbitMQ Server as follows.

RabbitMQ Server

Allow access to the Firewall, as shown below.

Firewall

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

Installation process

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

RabbitMQ Service

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

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

RabbitMQ Web Management

Now, enter the following command to enable Web Management.

  1. rabbitmq-plugins enable rabbitmq_management
    Web 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
  • web machine
  • 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.

RabbitMQ web page

By default, RabbitMQ runs on 15672 port No.

Now, the default user 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.

Delete Queues

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

 Windows Application project

And, here I have designed my Form as below.

Designed

Now, Under the Submit button click, and write the following code.

private void button1_Click(object sender, EventArgs e)
{
    var factory = new ConnectionFactory()
    {
        HostName = "localhost"
    };

    using (var connection = factory.CreateConnection())
    using (var channel = connection.CreateModel())
    {
        channel.QueueDeclare(queue: "MyQueue",
                             durable: false,
                             exclusive: false,
                             autoDelete: false,
                             arguments: null);

        string message = textBox1.Text;
        var body = Encoding.UTF8.GetBytes(message);

        channel.BasicPublish(exchange: "",
                             routingKey: "MyQueue",
                             basicProperties: null,
                             body: body);
    }

    MessageBox.Show("Message sent successfully.");
}

Note. To work with RabbitMQ, we need the following Namespace.

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.

Storing messages

UI and Server

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.

Check the queue

Here, if you 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.

MyQueue

Here is the overview of the MyQueue.

Overview

So far, we have successfully sent the message from the sender to the 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 doubts, you can give your feedback so that we can try to explain it to you more clearly.


Recommended Free Ebook
Similar Articles