django-websocket-redis icon indicating copy to clipboard operation
django-websocket-redis copied to clipboard

Is there any way to retrieve the connected users?

Open YuriHeupa opened this issue 10 years ago • 7 comments

How can i retrieve all the users connected to the server? I need to check if user is online to choose the appropriate behavior

YuriHeupa avatar Jan 06 '15 14:01 YuriHeupa

Currently not.

But this feature could be added and it makes sense. I would do it, so that Redis keeps a list of currently connected users. Then the Django loop can check if a users is in that list.

However, I think you can do that reliably only through the clients send_heartbeat. This means that you would add that user to a Redis bucket, which times out after say 60 seconds. Whenever a heartbeat message is received from the client, this timer is reseted.

Before implementing this, I'd like to ask you, how quickly you need that information. If its >3*heartbeat_interval, then I would say yes. If you need it immediately, then its unreliable.

jrief avatar Jan 06 '15 16:01 jrief

This will be a nice approach, but if there was an way to get the user on the request while the socket connect/disconnect would be easier to handle this

YuriHeupa avatar Jan 06 '15 16:01 YuriHeupa

Whenever he connects yes, but ws4redis does not see reliably whenever he disconnects. If he really closes the browser window, then the connection is closed in a clean way. But if the browser looses TCP/IP connectivity, then that connection remains open. Therefore you can't reliably say, whenever a users disconnected - you can only say that you missed some heartbeat messages and therefore you assume he is not online anymore.

jrief avatar Jan 06 '15 17:01 jrief

I did with the client send_heartbeat, whenever a heartbeat message is received from the client, the server updates the client last_seen field, with this i consider user online if last_seen time is 5 seconds less than actual time, the only problem i'm sending heartbeat through http, i'd like to send it over websocket, but i don't found way to receive the websocket message event inside django loop

YuriHeupa avatar Jan 07 '15 12:01 YuriHeupa

@YuriHeupa You can hack into the code here: https://github.com/jrief/django-websocket-redis/blob/master/ws4redis/redis_store.py#L68

I jump from there back to my django code and do the black magic. (Attention: Keep in mind, no blocking operations or you do monkey patching!)

I think there should be an option like WS4REDIS_ALLOWED_CHANNELS which can be used for heartbeat operations.

CPlusPlus17 avatar Jan 19 '15 13:01 CPlusPlus17

+1

tarunbehal avatar Apr 21 '15 06:04 tarunbehal

Can someone pls help with this issue? I am unable to figure out how to implement it. Thanks.

onkarmukherjee avatar Feb 18 '19 06:02 onkarmukherjee