How decrease disconnect detection time in SignalR?

Zuka Esakia 20 Reputation points
2023-08-09T11:55:54.0933333+00:00


Hello, i am working on .net framework and in our web app we are using SignalR connection for transmitting data and etc. We want to catch client disconnection as soon as possible using signalr hub methods (OnDisconnected or if there is any other).I read the this documentation about connection lifetimes Understanding and Handling Connection Lifetime Events in SignalR | Microsoft Learn and i decreased time at minimum for this parameters in global.asax file as it was mentioned in docs.

Parameters - GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(2);   (2 is minimum)
                     GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(6); (6 is minimum)
But this does not work properly for us. Disconnect detection in real life needs way more time(25-30 secs) than it is configured by parameters. Please help me to find problem, are we doing something wrong or just this is how it works. If there is alternatives please advice us.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,575 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,397 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
131 questions
{count} votes

Accepted answer
  1. JasonPan - MSFT 4,796 Reputation points Microsoft Vendor
    2023-08-09T13:52:54.5166667+00:00

    Hi @Zuka Esakia,

    There is no reliable method of detecting that the cable has been unplugged and pending HTTP requests might not be terminated if this happens.

    Related Links

    https://github.com/SignalR/SignalR/issues/3919#issuecomment-297896803

    SignalR bare WebSocket. How to detect that client lost internet connection

    But we can change the retry Count inside the withAutomaticReconnect to decrease disconnect detection time.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason Pan

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 60,386 Reputation points
    2023-08-09T16:19:31.5333333+00:00

    first the keep-alive is only used for http not web sockets. so it only affects signal/r when ajax polling is used instead of web sockets. when polling you want the keep-alive greater than the poll interval.

    there is no reliable way to detect a lost tcp connection except by sending a message that fails.

    If you need fast detection of lost connection then the client and server need to ping each other. this of course increases traffic. so, for a 6 second server detection, your server needs to ping the client at least every 6 seconds to verify the connection is valid.

    0 comments No comments