Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - Handling disconnected users

Tags:

signalr

Hy,

I'm using the signalR library on a project to handle an notification and chat modules. I've a table on an database to keep a track of online users.

The HUB for chat is inheriting IDisconnect where i disconnect the user. After disconnecting the user, i warm the users about that event. At this point, i check if the disconnect user is the client. If it's, then i call an method on HUB to reconnect the user (just update the table).

I do this because with the current implementation, once the user closes a tab on the browser it calls the Disconnect task but he could have another tab opened.

I've not tested (with larger requests) this module yet, but on my development server it could take a few seconds between the IDisconnect event, and the request from the user to connect again.

I'm concerned with my implementation to handle disconnected users from the chat but i can't see another way to improve this.

If possible, could someone give me a advice on this, or this is the only solution that i've?

Update: I ended up using a singleton class to store all the users and their connections id from signalr. This way i can get the id from user during the disconnect task (at this point you don't have any httpcontext to get the user information, but you can always get the user id with the connection id of signalr from the array in the singleton class).

20-02-2013 Although the above solution was doing the job, i had the need to scale my project. My solution was to use Redis to store all user connections, and take benefit of key expiration time on disconnect events. During the reconnect i check if the key is in pending state (gonna expire in a few minutes).

like image 714
Gui Avatar asked Sep 02 '12 08:09

Gui


1 Answers

You can check out how JabbR, a multi-room chat application built on top of SignalR, solves this problem: https://github.com/JabbR/JabbR/blob/master/JabbR/Hubs/Chat.cs

It basically keeps a 1:N mapping of User<->ConnectionId, so when the last connection is disconnected the user can be marked as "offline".

like image 136
Alexander Köplinger Avatar answered Oct 04 '22 14:10

Alexander Köplinger