Message broker – RabbitMQ basic functions

Before I’ll explain basic function of RabbitMQ, in the first place we need to know what it is “Message broker”.

Message broker

Via wikipedia
In computer programming, a message broker is an intermediary program module that translates a message from the formal messaging protocol of the sender to the formal messaging protocol of the receiver. Message brokers are elements in telecommunication or computer networks where software applications communicate by exchanging formally-defined messages. Message brokers are a building block of message-oriented middleware.

In image below you can see what is the difference in communication between Message Broker and REST.


In brief in microservice approach in rest(if we don’t use pipeline) we communicate with service each other. We send requests between services and gateways. With message broker we publish some message and we subscribe to it. The broker is responsible for communications.

Example of use

In this example I’ll use RabbitMQ with EasyNetQ client. The performance of our application is simple.I want to send product information from one service to another.
How to configure and install RabbitMQ.

At the beginning we need to create three projects one will be Publisher (Api Core), Subscriber (Console Core) and the last but not least “contractor”(class library core).

At first I show you a class that will be a contract between our services.

Product class:

public class Product
    public string ProductName { get; set; }
    public decimal Price { get; set; }

We need to create post action that will publish message to broker from API.

public IActionResult Post([FromBody] Product order)
    var messageBus = RabbitHutch.CreateBus("host=localhost");

    return View();

In the 4 line I connect with local rabbitmq, next I’ve just published product to message broker.

Now we have to subscribe to the message product in our console service.

class Program
    static void Main(string[] args)
        var messageBus = RabbitHutch.CreateBus("host=localhost");
        messageBus.Subscribe<Product>("SubscriptionId", msg =>
                Console.WriteLine($"Product :{msg.ProductName} costs {msg.Price}");

Let’s see how it works. I first send a product class to an application that is publisher via post.
postThe console application correctly received the message and displayed it on the console.


We also have access to the administration panel rabbit mq which runs on the default port 15672 and track how long the message arrives.



I hope that in this post I clarified the subject of message brokers and rabbitmq on basic level.
Link to project.

Leave a Reply

Your email address will not be published. Required fields are marked *