SETTING UP A FANOUT QUEUE IN RABBITMQ

What is a fanout queue?

A fanout queue sends one message that gets distributed to all queues that are listening.
Since there are many queues that receives messages, we need a single point where the queues fetches the messages. This is the Exchange.
If the queue is connected to the Exchange, it receives the message. No addressing or filtering of the messages.
For more info about the different queues, look here

For more info about the different queues, look here: All RabbitMQ Articles.

NuGet

To use a RabbitMQ client you only need one NuGet package.

The code

This is basically what is needed to create a fanout queue. You do this in both the sending and receiving clients.

private IAsyncConnectionFactory _factory;
private IConnection? _connection;
private IModel? _channel;

public void CreateQueue()
{
    _factory =
        new ConnectionFactory
        {
            HostName = "localhost",
            UserName = "guest",
            Password = "guest",
        };

        _connection = _factory.CreateConnection();
        _channel    = _connection.CreateModel();
        _channel.ExchangeDeclare("FanoutExchange", ExchangeType.Fanout);
}

The code explained

_factory =
    new ConnectionFactory
    {
        HostName = "localhost",
        UserName = "guest",
        Password = "guest",
    };

The factory is where all entities that RabbitMQ uses are created.
It is also here you tell the client where the RabbitMQ server is, and what account to use.

HostNameThe IP address to the RabbitMQ server.
UserNameThe name of the user that is used to gain access to the server.
PasswordThe password that verifies the user.

The default user name and password is guest/ guest.

_connection = _factory.CreateConnection();

This is basically the TCP connection to the server. This is where all the queues will be sending its data. Since it is created from the factory, it knows what server to create a connection to.

_channel = _connection.CreateModel();

From the RabbitMQ documentation
https://www.rabbitmq.com/channels.html

Some applications need multiple logical connections to the broker. However, it is undesirable to keep many TCP connections open at the same time because doing so consumes system resources and makes it more difficult to configure firewalls. AMQP 0-9-1 connections are multiplexed with channels that can be thought of as “lightweight connections that share a single TCP connection”.

_channel.ExchangeDeclare("FanoutExchange", ExchangeType.Fanout);

This is where the exchange is created.
We declare the name of the Exchange. This is the entity that “connects” the sender and receivers.
The type is “fanout”, which means that no filtering will be used, all messages that are sent to the server is also distributed to all queues and receivers.

Additional code for the subscriber

private string? _queueName;

public void BindQueueToExchange()
{
    _queueName = _channel.QueueDeclare().QueueName;
    _channel.QueueBind(_queueName, "FanoutExchange", routingKey: "");
}

The code explained

private string? _queueName;

The queue name is a member because, in this setup that I describe in these articles, it is reused when we receive the messages too.

_queueName = _channel.QueueDeclare().QueueName;

We’re actually not interested in what the actual name is, so it’s convenient to let the system create it for us.

_channel.QueueBind(_queueName, "FanoutExchange", routingKey: "");

To receive messages, we have to let the exchange know that this is the queue we are listening to. This is done by binding it to the exchange.

Just make sure that the name of the exchange is the same as the sender.

— Cheers!

Like it? Share it!

Leave a Reply

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