I set myself a challenge today to write a jQuery chat in under half an hour. It eventually took me 40 minutes.
However, I want to improve it so the load on the server and browser isnt horrendous.
Currently the user types into a text box, presses enter, this data is sent to a .php file which updates a mySQL table and outputs all the rows on the table.
There is a set Interval on the div every two seconds to update if anyone said anything without the user pressing enter.
I just wanted to know thoughts on how to do perform this in a better way, or the most efficient way. I want to understand the best technology to use and why.
Thanks for all your input, I love stack overflow, its been invaluable to me.
Using ajax polling for a chat application with the "php back"/"javascript front" technology stack will inevitably result in a heavy server load. Http is just not designed for that kind of communication, and if you're using apache (as I assume) there is a really heavy overhead for each request.
As some of the commments indicated, you could investigate using a full stack javascript framework (i.e. Node.js on the backend).
When I had the task of accelerating an existing chat application with php backend and Javascript frontend (using periodic ajax poll), I ended up using a 3rd party server side product to handle lightweight XMPP requests. This server side product was OpenFire, but you could use eJabberd for even better performance - OpenFire is easier to set up and maintain though. The results were more than satisfactory, server load dropped significantly, and messages were delivered instantly for ~1000 online users chatting wildly away (on a less-than-average performance dedicated linux box).
It's hard to explain all the tiny details within a SO answer's scope, but luckily Ben Werdmuller @ IBM went out of his way to write an awesome tutorial on this topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With