C# Service Bus multiple listeners all receive the same message ( BrokeredMessage)
Asked Answered
A

1

6

I used to use RabbitMQ as a messaging platform and I never had any issues with it - unfortunately, we've recently moved our infrastructure over to Azure and they don't provide RabbitMQ servers so I thought about trying out the Service Bus extension.

I have one writer and multiple readers. Currently, the readers will each read a different message (message competing pattern - e.g. good for load balancing).

What I want, is that all the readers get the same message and process it themselves.

The reader(s):

string connectionKey = "....";
        this.client = QueueClient.CreateFromConnectionString(connectionKey, "dev");
        this.client.OnMessage((message) =>
        {
            try
            {
                Console.WriteLine("Message received: " + message.GetBody<string>());
                Console.WriteLine("Message ID: " + message.MessageId);
              //  message.Complete();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception " + e.Message);
            }
        });

The writer:

Console.WriteLine("Sending message " + message);
        BrokeredMessage msg = new BrokeredMessage(message);
        this.client.Send(msg);

I've been looking for a solution for two hours and couldn't find anything. In RabbitMQ, that would be the default behavior.

Image showing pattern of what I have vs. what I need

Alyosha answered 21/5, 2016 at 19:34 Comment(0)
V
6

Azure Service Bus supports multiple paradigms, one of which is called "Topics":

How to use Service Bus topics and subscriptions

In contrast with Service Bus queues, in which each message is processed by a single consumer, topics and subscriptions provide a "one-to-many" form of communication, using a publish/subscribe pattern.

Link above demonstrates core topics concepts as well as code samples.

Venial answered 21/5, 2016 at 20:24 Comment(4)
It seems my code works if I use 1 subscription for each listener. Is this correct? So if I have 100,000 users, I will have 100,000 subscriptions ?Alyosha
@Alyosha What do you consider a reader is up to you. There are other technologies that can help you out if you are looking for a way to do a broadcast. One of them is SignalR. Are you familiar with how it works? You could theoretically pull message out of the queue or topic and use SignalR to broadcast that message to all or specific group of connected users. If your users are mobile users, you could go as well with Azure Notification Hub and custom tags.Helfand
I read about SignalR while looking for a solution. My setup doesn't require a lot of connected consumers, i was just wondering if having multiple subscriptions is the way Topics are supposed to work or if I was doing something wrong. Nevermind Topics are fine for me ;)Alyosha
@AdmirTuzović In the event-driven architecture, an application would want to handle the same event in multiple different ways. How do you achieve that with the topics and subscribers? Manually create all subscribers ahead of time?Crackle

© 2022 - 2024 — McMap. All rights reserved.