I added the chat feature to the site using jquery and PHP, and it seems to work well, but I'm worried about scalability. I wonder if anyone has any advice. The key area for me, I think, is effectively managing the awareness of who is onine.
detail: I haven't implemented a long poll yet (yet), and I'm worried that the raw number of lengthy processes in PHP (Apache) is getting out of hand.
My code runs periodic jQuery ajax poll (4secs), which first updates db to say that I am active, and sets a timestamp. Then there is a procedure that checks the timestamp for all active users and disables it outside (10 minutes). So far, this is pretty normal. However, I agree that if I allow every active user to check every other active user, and then everyone updates db to start inactive users, then I will get duplication of efforts, blocking records and unnecessary server loading.
So, I realized the idea of ββthe role of a "sweeper". This is just one of the online users who inherits the role of the person performing the cleanup. Everyone else just checks to see if a "sweeper" exists (reading the database) and continues. If during the inspection there is no sweeper, they make themselves sweeping (DB write for their record). If there are more than one, make yourself a βnon-sweeper,β sleep for a random period and check again.
My theory is that in this way there is only one user who regularly writes updates to several records in the corresponding table, and all the others either read or simply write their own record. Thus, it works fine, but the problem is that the process requires several readings of the database and may actually be less efficient than just letting everyone clean up, as in other studies, as I mentioned.
I have had more than 100 concurrent users working so far, but the client wants to scale to several 100, even more than 1000, and I donβt know if at the moment I know whether this idea is good or not.
Does anyone know if this is a good approach or not, is it scalable for hundreds of active users, or can you recommend a different approach?
Aside, a long poll / comet for actual chat messages seems simple, and I found a good resource for the code, but there are a few blog comments that suggest this is dangerous for PHP and apache. active threads, etc. Impact is reduced with usleep and session_write_close.
Again, someone has the hands-on experience of a long PHP survey designed for hundreds of active users, maybe you can calm down! Should I really try to port this to node.js (without experience)?
Thank you in advance
Tony