title: What SignalR method is called when the connection is lost or is there a handler for connection state changes?
body: <p>I'm working on a .NET web app using SignalR with the hub class similar to the example class below:</p>
  
  <pre><code>public class ContosoChatHub : Hub
  {
      public override Task OnConnected()
      {
          // Add your own code here.
          // For example: in a chat application, record the association between
          // the current connection ID and user name, and mark the user as online.
          // After the code in this method completes, the client is informed that
          // the connection is established; for example, in a JavaScript client,
          // the start().done callback is executed.
          return base.OnConnected();
      }
  
      public override Task OnDisconnected()
      {
          // Add your own code here.
          // For example: in a chat application, mark the user as offline, 
          // delete the association between the current connection id and user name.
          return base.OnDisconnected();
      }
  
      public override Task OnReconnected()
      {
          // Add your own code here.
          // For example: in a chat application, you might have marked the
          // user as offline after a period of inactivity; in that case 
          // mark the user as online again.
          return base.OnReconnected();
      }
  }
  </code></pre>
  
  <p>More specific, my web app serves as hub for connecting tablets. when i close the app on the tablet it does not trigger instantly the OnDisconnected task, taking up to 20 seconds or more (server tries to reconnect with the client).</p>
  
  <p>My question is, which method should I use in order to detect the connection loss as soon as it happens or, is there a connection state handler that triggers when the connection is lost?</p>
  
  <p>In order to prevent the data loss (considering a tablet online when in fact it's not) I really need to handle the disconnecting event.</p>
  
  <p>Any help is much appreciated!</p>
  
  <p>Later edit:
  I've also included the following lines in the Global.asax file</p>
  
  <pre><code>GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(6);
  GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(2);
  </code></pre>
  
  <p>in the Application Start method. The values seem to be saved, as seen in debug and actually reduce the time by half, from 20-30 to 12-14 seconds, but it's still not  as close to 2-3 seconds.</p>