Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery Chat with PHP mySQL

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.

like image 286
Chud37 Avatar asked Nov 04 '22 19:11

Chud37


1 Answers

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.

like image 169
András Szepesházi Avatar answered Nov 12 '22 15:11

András Szepesházi