Track websockets that are closed by the browser
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
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?
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.
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?
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 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 @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.
@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.
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
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.
+1