Search code examples
.netrabbitmqsignalrevent-buseasynetq

Multiple subscribers easymq


I have a .NET application using EasyNetQ for listening to events published from another application. The application uses SignalR to publish the received events to a dashboard.

I am using octopus deploy to deploy my application and the problem is that every time a new version of the application is deployed I stop receiving all messages posted on the bus (it seems that i get some of them but not all). I have read about having multiple subscribers with the same id and I'm guessing this is the problem.

How should I handle this to avoid "loosing" messages?

And how can I run different instances of the same application (for example several developers working against the same rabbit mq instance) and all clients receiving all messages?


Solution

  • If you have multiple front-end instances and you want to use EasyNetQ as a 'backplane' for SignalR, make sure the subscriber ID's (=queue names) are unique for each instance.

    For load balanced IIS servers you could for example include the machine name, or if you really want to miss the least amount of messages you could include the process ID. (with IIS there can be multiple processes active for the same AppPool, during a recycle for example).

    I always set the TTL for the queue on something like 5 minutes, so you don't end up with orphaned queues.

    You can set the TTL for a queue by specifying WithExpires on the subscription, so something like this:

        var subscriptionId = $"{Environment.MachineName}[{Process.GetCurrentProcess().Id}]-mymessage";
        Bus.Subscribe<MyMessage>
        (
            subscriptionId, 
            message => Console.WriteLine(message.ToString()), 
            subscriptionConfig => subscriptionConfig
              .WithExpires((int)5.Minutes().TotalMilliseconds)
        );
    

    There's a similar overload when you use the AutoSubscriber