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

Track websockets that are closed by the browser

Open lonemc opened this issue 11 years ago • 10 comments

Hi jrief,

Great job with this project. Are you planning to implement in the future for a way to tell Django when a socket is closed from the client/browser side? I am trying to keep track of the django users that have a websocket connection open at any given time.

Thanks

lonemc avatar Mar 11 '14 22:03 lonemc

This only makes sense for websockets connected using session as audience. In that case, then yes, it could be implemented. I could imagine to create a special list in Redis, containing all the session-id's, of connected clients. If one of those clients disconnects, that session-id disappears from the list. Do you think such an implementation makes sense for you?

jrief avatar Mar 11 '14 23:03 jrief

You are right. Tracking the websockets by their sessions instead of users makes more sense. Saving the session-id's in a Redis list sounds like a good plan too but it only solves half of my problem, since I still need to check that list and then update the django database accordingly. How about having a WEBSOCKET_ON_CLOSE option in the settings where one can define a callback function for ws4redis to call when a websocket closes? One of the parameters of that function could be the websocket's saved session.

lonemc avatar Mar 11 '14 23:03 lonemc

I strongly recommend against any database stuff inside the websocket loop. Its even dangerous to retrieve a User using the session-id, but at least, Django caches these objects. A callback would give programmers too many hooks to ignore this rule.

But why do you need to know immediately, when the client disconnects? Isn't it enough to find out that information the next time a Django does something?

jrief avatar Mar 12 '14 06:03 jrief

I am trying to make a web-based chat app. If someone leaves the chat channel, by closing the browser and subsequently the websocket, I would like to automatically notify the other users and also update the server about who is online and who is not.

lonemc avatar Mar 12 '14 15:03 lonemc

@lonemc Why dont you use a long running celery task for example? It could check every few seconds for a change in the redis session-list and act accordingly, i.e. broadcasting a message to all remaining chat-users.

Morpho avatar May 28 '14 14:05 Morpho

@Morpho @lonemc The websocket loop gets notified, if the (web)socket is closed. However, this event currently is not propagated to any business logic. It could easily be done though.

jrief avatar May 28 '14 20:05 jrief

@Morpho Implementing a special redis session-list with the opened/active web-sockets is one possible solution, as also suggested by @jrief in his post from Mar 11. Then it will be the responsibility of the developers to periodically check that list for closed/unused sockets.

lonemc avatar May 29 '14 14:05 lonemc

Hi, First, thanks for this project, it is of great help for mine.

I have the same need as Ionemc. Has someone already implemented this feature, or is the issue still not handled ? If so, I could give a hand, but although I am familiar with Python and already worked with Django, it is the very first time I deal with Redis, so I might not be the best one to implement properly this idea.

F.dG

Frky avatar Dec 29 '14 15:12 Frky

@Frky

it is the very first time I deal with Redis, so I might not be the best one to implement properly this idea

Before starting this project, I never used Redis before. The API is quite simple, so you should be able to proceed easily.

jrief avatar Dec 29 '14 16:12 jrief

+1

domface avatar Feb 01 '16 02:02 domface