2

I have several chat rooms. Currently, I store the list of chat users in a php variable. If a user enters or leaves the room, the user's name is added/removed from that list. To make this storage persistent, I use memcached. To update the status of the chat room and send the user list to all users in the chat room, I use periodical ajax requests that fetch the user list into the browsers of the users who are in the chat.

It works okay. But I doubt that sending the whole list of chat users to everybody every XX seconds is a good idea if there are a couple of hundred people in the chat.

How are chat rooms usually dealing with this problem?

user478419
  • 413
  • 2
  • 6
  • 20
  • 1
    Why not just send the whole list initially then each update send the delta? – John Hartsock Nov 29 '10 at 19:10
  • How do users get normal messages? – thejh Nov 29 '10 at 19:57
  • The users get the updates with a second process that is run more often than the one that updates the user list. I am a little worried about maintaining consistency if I only update with the differences. If for some reason one update does not get applied, there will be users showing that aren't even online. – user478419 Nov 29 '10 at 21:17

5 Answers5

2

Have no idea how they do, but here are some ways you could. Remember to measure before and after you optimize.

  • Version the list. Don't get the full list if you have an up to date version.
  • Diff the list. Send only the updates since the last update.
  • Log this list. Cache or push updates as new events occur.
Thomas Langston
  • 3,743
  • 1
  • 25
  • 41
  • I did not think about creating a version number. Sounds interesting. I thought about only sending the changes. If there's 300 people in the chat, it will sure make a difference to only send a few differences instead of the whole list. – user478419 Nov 29 '10 at 20:13
1

The way I run my chat rooms, I timestamp all the new messages and new sign ins and sign outs.

When a user enters a room, I download the full list. Periodically(via an AJAX with JSON call), I will download any new events (messages, logins and logouts). And update the relevant lists accordingly.

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
0

Honestly, what you are doing sounds pretty good. Keep doin what ya doin. Maybe keep track of the last list and only send an update if they have changed in the last x time? If you really want to save a few bytes.

DampeS8N
  • 3,621
  • 17
  • 20
0

I would push the list updates through the same channel that the chat messages go through, too. If it's an endless-loading page, maybe you could inject something like this in the page:

<script>updateUserlist({user:"alice", eventtype:"leave"});</script>
thejh
  • 44,854
  • 16
  • 96
  • 107
0

Thanks for all the suggestions. Additionally, I will look into running a comet server. The long-polling approach just would not scale well.

user478419
  • 413
  • 2
  • 6
  • 20