-1

I am trying to use EasyNetQ to send and receive messages from RabbitMQ, I need to send same message to multiple receivers or whom ever is connected at the time.

I have tried publish-subscribe messaging pattern and it works fine, but it's more like round-robin, where once the message is received by a receiver it's removed from the queue and nobody else can see the message.

Here's my message sender.

        for (int i = 0; i < 10; i++)
        {
            using (IBus bus = RabbitHutch.CreateBus(Host))
            {
                bus.Publish(new TextMessage { Text = $"{i}: Hello World from EasyNetQ" }, "dashboard");
            }
        }

Here's my receiver.

public void GetMessages() {

        using (IBus bus = RabbitHutch.CreateBus(Host))
        {
            bus.Subscribe<TextMessage>("dashboard", HandleTextMessage);

            Console.WriteLine("Listening for messages. Hit <return> to quit.");
            Console.ReadLine();
        }
    }

    static void HandleTextMessage(TextMessage textMessage)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Got message: {0}", textMessage.Text);
        Console.ResetColor();
    }

Once a message is received no other receiver can see that message and I need all of the connected receivers see the same message.

Peter
  • 19
  • 2
  • 6

3 Answers3

1

Actually EasyNetQ will abstract you from having to deal with exchanges in most cases. To achieve what you ask simply specify a different subscription Id when subscribing, for instace a GUID:

public void GetMessages() 
{

    using (IBus bus = RabbitHutch.CreateBus(Host))
    {
        bus.Subscribe<TextMessage>(Guid.NewGuid().ToString(), HandleTextMessage);

        Console.WriteLine("Listening for messages. Hit <return> to quit.");
        Console.ReadLine();
    }
}

static void HandleTextMessage(TextMessage textMessage)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Got message: {0}", textMessage.Text);
    Console.ResetColor();
}

More information here: https://github.com/EasyNetQ/EasyNetQ/wiki/Subscribe

Cristian T
  • 2,245
  • 3
  • 25
  • 45
0

I believe the question describes the behavior of a fanout exchange. They aren't widely used, but it's clear that is what you are looking for. More information can be found here: https://www.tutlane.com/tutorial/rabbitmq/csharp-rabbitmq-fanout-exchange

theMayer
  • 15,456
  • 7
  • 58
  • 90
  • Yes this is what I am looking for, but I was wondering if there's the same ability in EasyNetQ Client? – Peter Jan 06 '19 at 16:09
0

I did not understand how RabbitMQ works. All I needed was to create an Exchange and have Exchange send the messages to the queues. I though Queues did that.

Peter
  • 19
  • 2
  • 6