I have authored a little chat system using jquery, php, and mysql however, I am searching for some type of technology which will only update a if your new record is placed right into a row. I seem like using jquery ajax calls every second to retrieve new records is actually overkill and strenuous on my small server.

You're searching for a Comet solution: http://en.wikipedia.org/wiki/Comet_%28programming%29

The concept, as pdr noted, would be to the javascript continuously open an async request using the server. The server holds it open, but doesn't send anything until it determines there's something to transmit. The request will timeout around the javascript side after 10-20 seconds, and should re-open the bond.

This utilizes a 'subscriber' based model, through which the server will be sending the chat message or whoever else, to any or all clients that are activated, all at one time. This protects you a lot database demands, because the server may be the one requesting the demands, not the person clients.

What you would like is long polling. Essentially, you are making an XHR, and also the server and PHP holdes the request open until new data is able to be delivered back.

You have to configure Apache to not timeout within this conditions, so some substantial research. Essentially, the PHP appears like so...

set_time_limit(0);

while (TRUE) {

   $db->query('SELECT `message` FROM `messages` WHERE `new` = TRUE');

   if ($db) {
       echo json_encode($db->results());
       exit;
   }
   sleep(1);
}

Then, you are making an XHR with this page, and it'll stay open until new information is ready. Then, around the complete callback, improve your page's condition making a new XHR.

This is much more efficient than polling for updates constantly using XHR.

Make certain you perform a large amount of research because In my opinion Apache will think situations are wrong if your PHP script has not stopped after thirty seconds approximately. :)

You will find a few routes I understand of that you could take.

  1. Lengthy polling. This is when the browser opens an association towards the server and does nothing before the server responds. When the server responds or occasions-out (transmits a clear reaction to the browser), a brand new lengthy-polling request is created.

    When going this route, you need to use a server that doesn't depend on utilizing a new thread for every request.

  2. Web electrical sockets. Again, you will want a server that may handle demands without breeding a brand new thread every request. With web electrical sockets, an association is stored open between your client and servier, and in contrast to Lengthy polling, does not break. However, this is not well-supported yet.

I recommend looking at http://socket.io/

The purpose of Ajax is the fact that it's asynchronous. Are you able to not only wait in the server until there is a useful reaction to send?

With standard HTML/CSS/JS, that's virtually the only method because the browser could make demands from the server, not the other way around. The AJAX call should not need to be very large whatsoever. A chat system, obviously, will require striking the server a great deal.