views:

69

answers:

2

I'm using PHP, AJAX, a MySQL database, and a lot of jQuery to prototype web-based chat system (similar to Facebook Chat). I'm stuck on how to "listen" for incoming chats... when to know someone is trying to chat me... and to know that it is a new chat, not an existing chat.

Right now, I'm polling to see if there has been new insertions in the database tables but it seems very inefficient... a lot of overhead for the server.

Is there a way to receive a notification when, for example, a row has been inserted in a table in a MySQL database so that instead of having constantly poll, I can just be notified and then go look at what as inserted?

If there is a better and more efficient way to create this one-on-one chat relationship, please give me some suggestions.

Thanks, Hristo

+1  A: 

I think polling is the only way for JavaScript to be pushed server side changes.

alex
+4  A: 

You have to use polling, but you can use a technique called Comet which involves long-polling, i.e. sending out an ajax request that will be held by the server until a chat request comes in.

http://en.wikipedia.org/wiki/Comet_(programming)

Roy Tang
This is like the 5th time I've heard about Comet... probably time for me to check it out. Thanks. Can you give me some more specifics on the logic behind Comet?
Hristo
try http://ajaxpatterns.org/HTTP_Streaming or http://cometdaily.com/
quantumSoup
@hristo, Comet isn't a specific technology. It's the name given to any technique to simulate pushing data over the web. It's as much a technology as Ajax is.
Mike Sherov
Thanks for the explanation. I'll check it out
Hristo
+1 Hadn't heard the term before - sounds useful. This may be of use: http://www.dashdashverbose.com/2010/02/nodejs-websockets-stoopid-easy-comet.html
Jamie Wong