Pythonic / djangonic way to handle user timeouts in seconds (or minutes if necessary)

The closest example to my case is the Django ajax chat application. The rooms should have a list of active users. In addition to displaying this list of users in chat, this room can have a maximum number of active users; new users should be blocked from entering if there is no space.

I currently have a chat client page that invokes a poll view every second through ajax. The polling window displays the text of the room. I believe that polling can also do some type of pinging - adding users to the active_user M2M field on the Room object (I have it all in place so far). Now I need something that throws users from this active_user list after some timeout.

I assume there are two ways to do this, and I wonder what will be more efficient for an application that needs to be accurate for a second (or ten / fifteen seconds):

  • Using cookies / sessions / middleware a la this stream (however it seems to me that this method will not work for instant information
  • Another model, such as an explicit Users_Rooms table through a table with a datetime field for the time that is updated after creation and with each subsequent ping, and writes some function that clears the old

That’s all I can think of. I'm just trying to understand if mySQL defeat every second for every user for every room, this is a good idea and I wonder if this second is really the best option for this task. Thanks!

+4
source share
1 answer

I would use sessions and save the last ping datetime as a session parameter. Don’t worry about database hits, if at some point you feel that you are getting too many of them, just switch session storage to cache .

Make the application reliable for caching.

One of my friends used a fairly popular Django-based social network game on a fairly standard virtual private server, and it took some time before he was forced to switch to cache-based sessions.

+3
source

Source: https://habr.com/ru/post/1380509/


All Articles