Search code examples
c#azureredissignalrsignalr-backplane

Wiring up Redis with AspNet SignalR


I just wanted to validate my setting. It seems to work, but I thought to validate my setting with you guys.

I have SignalR hosted in my website. And in my Startup.cs I do:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);  //may be no longer needed because I've set KeepAlive?
        GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);
        GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);

        // following setting is get from https://greenfinch.ie/2015/07/09/azure-licks-using-redis-cache-signalr/
        var redisConnection = ConfigurationManager.ConnectionStrings["RedisCache"].ConnectionString;
        // set up SignalR to use Redis, and specify an event key that will be used to identify the application in the cache
        GlobalHost.DependencyResolver.UseRedis(new RedisScaleoutConfiguration(redisConnection, "MyEventKey"));

        app.MapSignalR();
    }
}

Now, I consume it like this in my other web service:

HubConnection hubConn = new HubConnection(url);
...
...
...
hubConn.CreateHubProxy("NotifyHub").On<string>("Notify", (msg) => PushToQueue(msg));
hubConn.Start();

Is this seems right? Is that means SignalR connection timeout is 10 seconds (because KeepAlive is set to 10 seconds)?

Here is my usage diagram (imagine there are lots of traffic to the website and webserver and Azure needs to fire up another computer for both website and webserver)

                        url: www.blablabla.com                 url: services.anotherweb.com
--------                      -----------                             ---------------
| User |  --> Modify Data --> | Website | --> Trigger Web Service --> | Web Service |
--------                      -----------          |                  ---------------
                                                   |                        ^
                                                   |                        |
                                                   |     --------------------  
                                                   |     |                    
                                                   ------|---------------------
                                                         |                    |
                                                         |                    |
                                                         |                    v
                        url: www.blablabla.com           |     url: services.anotherweb.com
--------                      -----------                |            ---------------
| User |  --> Modify Data --> | Website | --> Trigger Web Service --> | Web Service |
--------                      -----------                             ---------------

Sorry for the silly question. Just found out about redis today and wanted to scale my website with SignalR.


Solution

  • SignalR currently provides three backplanes: Azure Service Bus, Redis and SQL Server, we can use Redis to distribute messages across a SignalR application that is deployed on multiple instances, as you did, and your configuration is ok. This article:SignalR Scaleout with Redis is a detailed tutorial, you can refer to it.

    Is that means SignalR connection timeout is 10 seconds (because KeepAlive is set to 10 seconds)?

    SignalR uses one of transport APIs: WebSockets, server-sent events, forever frame, or long polling to create a transport connection. And for all transports other than long polling, the SignalR client uses a function called keepalive to check for loss of connectivity that the transport API is unable to detect, your configuration GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10) indicates a keepalive packet will be sent every 10 seconds.