Search code examples
javascriptasp.netsignalrsignalr-hubsignalr.client

SignalR handle timeout event on client side


I'm pretty new to SignalR and I have a web-app that communicates with the server using this technology (which is very cool IMHO).

What I'd like to know is - is there any way to handle a timeout event using SingalR on the client side? is there a timeout event being fired that I can listen to on the client side?

When I get a timeout, I can see it in he log:

[12:56:47 GMT+0300 (Jerusalem Daylight Time)] SignalR: Keep alive has been missed, connection may be dead/slow.
[12:56:54 GMT+0300 (Jerusalem Daylight Time)] SignalR: Keep alive timed out.  Notifying transport that connection has been lost.

Just to be clear I don't want to run my own timer and check whether a timeout occurred, I'm asking is there such an event that being fired in SignalR?

Thanks!


Solution

  • As you can read here $.connection.hub.disconnected

    Handle the disconnected event to display a message when an attempt to reconnect has timed out. In this scenario, the only way to re-establish a connection with the server again is to restart the SignalR connection by calling the Start method, which will create a new connection ID. The following code sample uses a flag to make sure that you issue the notification only after a reconnecting timeout, not after a normal end to the SignalR connection caused by calling the Stop method.

    You can manage the timeout of connection by $.connection.hub.disconnected event handler:

    $.connection.hub.disconnected(function() {
        if(tryingToReconnect) {
        notifyUserOfDisconnect(); // Your function to notify user.
     }
    });
    

    Pay attenction if you are using Long polling connection, because OnDisconnected() it is not called if the network connection suddenly drops.

    Long polling doesn't have the client side keep alive check and it can take several minutes for the browser itself to realize that the connection is really dead.

    Reference