Elkayam Gabay Elkayam Gabay - 1 year ago 83
jQuery Question

SignalR call OnDisconnect only after user closing the page/tab

So I'm using signalR to have online users list.
I used

to avoid that page refresh will be like disconnection, but now the user is never disconnecting..

public override Task OnDisconnected(bool stopCalled)
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindByName(Context.User.Identity.Name);
string name = currentUser.NickName;
if (!(stopCalled))
var item = users.SingleOrDefault(x => x.Nick == name);
if (item != null)
return base.OnDisconnected(stopCalled);

The user should only disconnect only when clicking on button (log off button)
or when his closing the tab/page.
What can i do?

Answer Source

stopCalled indicates whether the client disconnected gracefully or not, so your current method will remove only those users where the connections is gone (ex: network drop).

For a list of all online users, count the number of connections for each user.


  • if the user is not found in the list, add the user with count = 1
  • if the user is in the list, increment the count


  • decrease the count for that user
  • if for that user count = 0, remove the user from the list
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download