4

The closest example to my case is a Django ajax chat application. The rooms need to keep a list of active users. Aside from displaying that list of users within the chat, a given room might have a maximum number of active users; new users need to be blocked from entering if there's no room.

Currently I have the chat client page calling a poll view every second via ajax. The poll view returns the text of the room. I figure poll can just also do some type of pinging -- adding users to an active_user M2M field on the Room object (I have all of this in place so far). What I need next is something that drops users from that active_user list after some kind of timeout.

I imagine there's two ways to do this, and I'm wondering which would be more efficient for an app that needs to be accurate to the second (or ten/fifteen seconds):

  • Using cookies / sessions / middleware a la this thread (however it seems to me that this method wouldn't work for moment-to-moment info
  • Another model, such as an explicit Users_Rooms 'through' table with a datetime field for the time that is updated upon creation and with every ping following, and write some function that cleans up the old ones

That's all I can think of. I'm just trying to figure out whether hitting mySQL every second for every user for every room is a good idea, and wondering if that second is indeed the best option for the task. Thanks!

Community
  • 1
  • 1
floer32
  • 2,190
  • 4
  • 29
  • 50
  • 2
    Since chat sessions probably don't need to survive server reboots, and the number of participants is in low thousands per machine at best, you can store users' ping timestamps in a small in-memory key-value store. It could be a separate DB if you restart your Django processes often, or just be the cache as @Ivan-Kharlamov suggests. – 9000 Nov 10 '11 at 04:28

1 Answers1

3

I'd use sessions and store last ping datetime as a session parameter. Don't worry about database hits, if at the certain point you would feel that you're getting too many of them, just switch your session storage to cache.

Make your application robust to cache-flushing.

One of my friends used to host a relatively popular django-based social networking game on a pretty much standard virtual private server, and it took a while before he was forced to switch to cache-based sessions.

Ivan Kharlamov
  • 1,889
  • 2
  • 24
  • 33