Active PHP chat users

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

+6
source share
3 answers

A long survey is really very detrimental to PHP. PHP always works with limited parallel processes, and it will scale as long as you optimize to process each request as quickly as possible. A lengthy survey and similar solutions will quickly fill your pipe.

It can be argued that PHP is simply not the right technology for this type of thing, with current tools. If you insist on using PHP, you can try ReactPHP , which is the foundation for PHP, very similar to how NodeJS is built. The implication with React also is that it should run as a separate deamon, and not inside a web server such as apache. I have no experience in the stability of this and how scalable it is, so you will have to do the testing yourself.

NodeJS is not hard to get in if you know javascript well. NodeJS + socket.io makes it very easy to write a chat server and client using websockets. These are my recommendations. When I started with this, I had something good and worked for several hours.

+2
source

My advice would be to do this with a meteor , which should be pretty trivial even if you are not an expert, and then just upload such a chat to your PHP site via an iframe.

It will be scalable, will not consume a lot of resources, and in the future I will get only the best.

And this, of course, is superior to cometary PHP solutions, as well as jquery and ajax based on server requests.

I even think that you can find on github a more or less complete solution that just requires customization.

But of course, read the docs before you execute it.

If you are worried about security issues, read meteor safety

+3
source

If you want to keep your application stack using PHP, you want the chat application to work in your real web application (not iframe), and you are worried about scaling the real-time infrastructure, then I would recommend that you look at the hosted service for real-time updates time, such as Pusher , for whom I work. In this way, the hosted service handles the scaling of the real-time infrastructure for you and allows you to focus on creating the functionality of your application.

Thus, you only need to process chat message requests - to sanitize / check the contents, and then transfer information through Pusher to 1000 connected clients.

A quick start guide is available here: http://pusher.com/docs/quickstart

I have a complete list of hosted services on my real-time web technology guide .

+2
source

All Articles