Search code examples
phpmysqlajaxchatajax-polling

how to detect incoming chat message?


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


Solution

  • 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))