Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect SignalR Hub Client Disconnect instantly

When is the SignalR Hub OnDisconnected raised on server side, for the the .net client that crash or close without calling the Stop method?

I am testing with the SignalR .NET client, not the javascript client. If I call the Stop method on the client, the Hub will raise OnDisconnected method immediately.

But if I close the client or kill the process, the Hub will raise OnDisconnected method only after about 10 seconds.

How can I detect instantly that the client is disconnected?

like image 993
John Hpa Avatar asked Aug 06 '13 08:08

John Hpa


People also ask

What causes SignalR to disconnect?

Client disconnection scenariosWhen the user closes a browser window or tab, or navigates to a new page or refreshes the page, the SignalR connection immediately ends because SignalR client code handles that browser event for you and calls the Stop method.

How many concurrent connections can SignalR handle?

IIS on client operating systems has a limit of 10 concurrent connections. SignalR's connections are: Transient and frequently re-established. Not disposed immediately when no longer used.

How many clients can connect to SignalR?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

How do I stop SignalR connection?

hub. stop(). done(function() { alert('stopped'); });


2 Answers

Having read the documentation for SignalR events here, I spotted this section:

When a connection is inactive, periodically the server sends a keepalive packet to the client. As of the date this article is being written, the default frequency is every 10 seconds

There is a section that describes how to change the keepalive setting e.g.

protected void Application_Start(object sender, EventArgs e)
{
    // Make long polling connections wait a maximum of 110 seconds for a
    // response. When that time expires, trigger a timeout command and
    // make the client reconnect.
    GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);

    // Wait a maximum of 30 seconds after a transport connection is lost
    // before raising the Disconnected event to terminate the SignalR connection.
    GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);

    // For transports other than long polling, send a keepalive packet every
    // 10 seconds. 
    // This value must be no more than 1/3 of the DisconnectTimeout value.
    GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);

    RouteTable.Routes.MapHubs();
}

Thus you could look into reducing that value, in order to be notified quicker of when a client connection ha gone down.

like image 157
Jason Evans Avatar answered Sep 27 '22 21:09

Jason Evans


How can I detect instantly that the client is disconnected?

You can't, due to the way TCP works, until you try to send data to that client. As @JasonEvans' answer explains, SignalR by default sends data (a "keepalive" message) every ten seconds.

like image 34
CodeCaster Avatar answered Sep 27 '22 23:09

CodeCaster