rabbitmqrabbitmq-exchange

How can I set up exhange binding to multiple (wildcard) queues?


I've set up my fanout exchange hello.fanout.

Clients connecting and want hello messages connect to rabbitmq and declear queue hello.<GUID>. New clients will create new queues with the hello prefix in the name.

In my simple mind I thought I could create binding on the hello.fanout to hello.* queues but that is not possible?

Client queue:

enter image description here

Trying to add binding:

enter image description here

How can I get this to work? My thought was to create a publisher that publishes to and exchange and have unknown amount of clients connect and listen to the messages being published as I grow my application.


Solution

  • RabbitMQ Exchange deliver a message to one or multiple consumers (queues). This pattern is known as "publish/subscribe". (The queue name is not the catch here, you can use even aqueue with unknown name).

    There are 4 types of exchange: fanout, direct, topic and header.

    The fanout exchange is very simple. it just broadcasts all the messages it receives to all the queues that is bounded to it (no routing key need).

    The relationship between exchange and a queue is called a binding. so add a binding to all queues in the consumers.

    For example:

    channel.QueueBind(queue: queueName,
                      exchange: "hello.fanout",
                      routingKey: "");
    

    Sample of code:

    Publisher

    //declare an exchange
      channel.ExchangeDeclare(exchange: "hello.fanout", type: ExchangeType.Fanout);
    //Send a message to the exchange
                var message = GetMessage(args);
                var body = Encoding.UTF8.GetBytes(message);
                channel.BasicPublish(exchange: "hello.fanout",
                                     routingKey: "",
                                     basicProperties: null,
                                     body: body);
    

    Subscriber:

    //declare an exchange
      channel.ExchangeDeclare(exchange: "hello.fanout", type: ExchangeType.Fanout);
    //bind the queue to the exchange
                var queueName = channel.QueueDeclare().QueueName;
                channel.QueueBind(queue: queueName,
                                  exchange: "logs",
                                  routingKey: "");
    
    //declare a consumer event:
        var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body.ToArray();
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] {0}", message);
                };
    //add a consumer the a queue
                channel.BasicConsume(queue: queueName,
                                     autoAck: true,
                                     consumer: consumer);
    
    

    So if you want to deliver a message to multiple consumers. You should publish a message to your exchange, and bind all your consumers to the same exchange as the publisher.

    More information about exchanges and pub/sub in rabbitMQ website