2

I have a Windows Service that runs some processes and it must be notify the progress of it on the browser. I am not sure if I am doing something that is good but I just did it:

Windows Service publish a json on a redis channel called 'web' -> An action on ASP.NET MVC application subscribe the 'web' channel and send the json to browser via signalR hub -> the browser take it and show the progress.

I have the following code (it is a helper) to add a channel scope after a publish. It is called from my controller/action:

public void Listen(string channel, Action<string, object> action)
{
   var sub = Client.GetSubscriber();

   sub.Subscribe(channel, (c, v) =>
   {
      action(c.ToString(), v.ToString());
   });
}

The problem: It works as expetected and I get the browser notified. The problem is when the user (on browser) hits F5 or executes the action again. It creates a new channel and I get duplicated messages. If the users executes again it, I started getting 3 messages for each one and so on. I want to avoid it.

What I have tried: I tried to use the IsConnection(channel) but it always returns true. I have tried to Unsubscribe(channel) before Subscribe(channel) again and it works but I am not sure if i will lost some messages (I am afraid). I do not know how to solve it and avoid getting duplicate subscriptions. Does anyone can help me?

Thank you.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194

1 Answers1

0

Are you using the ConnectionMultiplexer? See Using redis pub/sub.

... in the event of connection failure, the ConnectionMultiplexer will handle all the details of re-subscribing to the requested channels

Consider switching from Pub/Sub to Redis Streams. See What are the main differences between Redis Pub/Sub and Redis Stream?

You can name groups and clients with Consumer Groups. Therefore you can control it by session, or anything else, or even use something like fingerprint.js to identify each browser anonymously.

LeoMurillo
  • 6,048
  • 1
  • 19
  • 34
  • Yes, the `Client` on my code is a `ConnectionMultiplexer`. Maybe it is create a new subscriber and duplicating the notifications to signalR. That's what I want to avoid and I do not know how. I think the `Pub/Sub` is my case because I just want to send the messages and done. – Felipe Oriani Jan 02 '20 at 20:18
  • How are you starting the `ConnectionMultiplexer`? You should probably create it as singleton in Startup.cs and dependency-inject it to the controller. – LeoMurillo Jan 13 '20 at 10:21