title: When is the connection closed with SignalR from browser
body: <p>I'm making a little chat application with SignalR 2.0 (just like everyone).</p>
  
  <p>I have a win8.1 application and when the application is closed, the hub receives the OnDisconnected event and removes the user from the list on the hub.
  The hub sends to every client that the user has left the chat, so we can visualize that the user has left.</p>
  
  <p>But when I'm using SignalR and Javascript in a webpage and the page gets closed, the hub doesn't get notified that the tab/browser is closed...</p>
  
  <p>Anyone any idea how the connection can be closed?</p>
  
  <p>What i've coded:</p>
  
  <p><strong>Startup Hub</strong></p>
  
  <pre><code>[assembly: OwinStartup(typeof(ChatServer.Startup))]
  
  namespace ChatServer
  {
  public class Startup
  {
      public void Configuration(IAppBuilder app)
      {
          // Map all hubs to "/signalr"
          app.MapSignalR();
      }
  }
  }
  </code></pre>
  
  <p><strong>Hub</strong></p>
  
  <pre><code>[HubName("ChatHub")]
  public class ChatHub : Hub
  {
      private static List&lt;User&gt; Users = new List&lt;User&gt;();
      public void Send(string name, string message)
      {
          // Call the broadcastMessage method to update clients.
          Clients.All.broadcastMessage(name, message, DateTime.Now);
      }
  
      public void UserConnected(string name)
      {
          Clients.Caller.connected(JsonConvert.SerializeObject(Users));
          User user = new User() { Name = name, ConnectionID = Context.ConnectionId };
          Clients.Others.userConnected(JsonConvert.SerializeObject(user));
          Users.Add(user);
      }
  
      public void UserLeft()
      {
          if(Users.Any(x=&gt;x.ConnectionID == Context.ConnectionId))
          {
              User user = Users.First(x =&gt; x.ConnectionID == Context.ConnectionId);
              Users.Remove(user);
              Clients.Others.userLeft(JsonConvert.SerializeObject(user), Context.ConnectionId);
          }
      }
  
      public override System.Threading.Tasks.Task OnDisconnected()
      {
          // Only called when win8.1 app closes
          // Not called when browsers closes page
          UserLeft();
          return base.OnDisconnected();
      }
  }
  </code></pre>
  
  <p><strong>HTML - Javascript:</strong></p>
  
  <p>Javascript:</p>
  
  <pre><code>chat = new function () {
  
  var ChatHub;
  // Connecting to the hub
  this.attachEvents = function () {
      if ($.connection != null) {
          ChatHub = $.connection.ChatHub;
         $.connection.hub.start({ transport: 'auto' }, function () {
              // Register client on hub
              ChatHub.server.userConnected("web name");
          });
      }
  
      this.send = function (name,message) {
          if ($.connection != null) {
                  //Send chat message
                  ChatHub.server.send(name, message).fail(function (e) {
                      alert(e);
                  });
          }
      }
    }
  };
  
   window.onbeforeunload = function (e) {
      //This is called when we close the page
      $.connection.hub.stop();
      return "You killed me! :'(";
  };
  </code></pre>
  
  <p><strong>Win8.1 client</strong></p>
  
  <pre><code>    internal async void ConnectToHub(string userName)
      {
          try
          {
              HubConnection hubConnection = new HubConnection(SERVER_PROXY);
              chat = hubConnection.CreateHubProxy("ChatHub");
              Context = SynchronizationContext.Current;
  
              MakeHubFunctionsAvailableOnClient();
  
              await hubConnection.Start();
              // Register client on hub
              await chat.Invoke("UserConnected", userName);
          }
          catch (Exception)
          {
              throw;
          }
      }
  
      private void MakeHubFunctionsAvailableOnClient()
      {
          //Receive chat messages
          chat.On&lt;string, string, DateTime&gt;("broadcastMessage",
                  (name, message, date) =&gt; Context.Post(
                      delegate {
                          Messages.Add(new UserMessage() { Message = message, User = name, Date = date }); 
                      }, null
                  )
          );
  
          //Receive all online users
          chat.On&lt;string&gt;("connected",
                  (users) =&gt; Context.Post(
                      delegate
                      {
                          List&lt;User&gt; userList = JsonConvert.DeserializeObject&lt;List&lt;User&gt;&gt;(users);
                          foreach (User user in userList)
                          {
                              Users.Add(user);
                          }
                          Messages.Add(new UserMessage() { Message = "CONNECTED", User = "System", Date = DateTime.Now }); 
                      }, null
                  )
          );
  
          //New user connected
          chat.On&lt;string&gt;("userConnected",
                  (user) =&gt; Context.Post(
                      delegate
                      {
                          User newUser = JsonConvert.DeserializeObject&lt;User&gt;(user);
                          Users.Add(newUser);
                          Messages.Add(new UserMessage() { Message = newUser.Name + " CONNECTED", User = "System", Date = DateTime.Now }); 
                      }, null
                  )
          );
  
          //User left, remove user from list on client
          chat.On&lt;string&gt;("userLeft",
                  (user) =&gt; Context.Post(
                      delegate
                      {
                          User newUser = JsonConvert.DeserializeObject&lt;User&gt;(user);
                          User y = Users.First(x=&gt;x.ConnectionID == newUser.ConnectionID);
                          bool ux  = Users.Remove(y);
                          Messages.Add(new UserMessage() { Message = newUser.Name + " left the conversation", User = "System", Date = DateTime.Now });
                      }, null
                  )
          );
      }
  </code></pre>
  
  <p>My hub doesn't trigger OnDisconnected, when i close tab / browser / navigating to other site
  The site is a singe page website (for the moment)</p>
  
  <p>What browser am i using? <br />Chrome: Version 32.0.1700.68 beta-m
  <br/>Internet Explorer 11 </p>