Search code examples
asp.netredishtml-tablereal-timepublish-subscribe

How do I use Redis Pub/Sub data to update html table in an ASP.NET application in realtime?


I have a webservice which connects to a Redis Subscription as follows:

using (RedisClient rc = new RedisClient("Publisher IP", 6379))
        {
            using (RedisSubscription rs = new RedisSubscription(rc))
            {
                rs.OnMessage = (channel, msg) =>
                    {
                        //msg is an object in json format to be displayed in html table
                    };
                rs.SubscribeToChannels("Channel Name");
            }
        }

Things I have tried so far:

  • I am not being able to use AJAX calls to get the data because the execution does not exit the rs.OnMessage code block.
  • It runs there in a loop until the subscriber disconnects from the publisher. I tried to store the msg value in Session and then call that value in the web page. The Session value does not seem to update however.

I am willing to try other techniques as well. Since I have just started with this project, I can start from scratch as well. Any suggestions on how I can do this?


Solution

  • For anyone, who might stumble onto this. I finally managed to do it by storing the most recent value from the subscription in a Redis key value pair (Hset), and then ran an AJAX call to retrieve that value in a loop. A list can also be used if you want some kind of cache or if you want to perform some operations received from the publisher. However, managing the list so as to not create a lot of overhead is a tricky task.