tags:

views:

136

answers:

4

Hi,

I want to create some kind of AJAX script or call that continuously will check a MySQL database if any new messages has arrived. When there is a new message in the database, the AJAX script should invoke a kind of alert box or message box.

I’m not quite a AJAX expert (yet anyway) and have Googled around to find a solution but I’m having a hard time to figure out where to begin. I imagine that it is kind of the same method that an AJAX chat is using to see if any new chat-message has been send.

I’ve also tried to search for AJAX (httpxmlrequest) call in a continuously and infinity loop but still haven’t got a solution yet.

I hope there is someone, which can help me with such a AJAX script or maybe nudge me in the right direction.

Thanks Sincerely Mestika

A: 

Step 1 - You need a server-side page that you can call that checks to see if something new has arrived.

Step 2 - You could adapt the sequential AJAX request script from here (it uses jQuery to simplify the AJAX requests):

http://www.stevefenton.co.uk/Content/Blog/Date/201004/Blog/AJAX-and-Race-Conditions/

Currently, this script is for queuing a list of sequential AJAX requests, but you could use it to continually check by changing it like this...

var InfiniteAjaxRequest = function (uri) {
    $.ajax({
        url: uri,
        success: function(data) {
            // do something with "data"
            if (data.length > 0) {
                alert(data);// Do something sensible with it!
            }
            InfiniteAjaxRequest (uri);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError);
        }
    });
};

InfiniteAjaxRequest ("CheckForUpdate.php");

What are the benefits of using this script?

Well, rather than checking every "x" seconds, it will only check once the previous request has been received, so it chains the requests. You could add in a delay to throttle this constant request, which I would highly recommend you do - otherwise you will be hitting your site with way too much traffic. You would add that delay in AFTER you've done something with the response, but BEFORE you call back into "InfiniteAjaxRequest".

Sohnee
+2  A: 

Here's your nudge:

  • Get one of the available JavaScript frameworks (jQuery seems to be the most common, but there are others)
  • flip though the documentation on the AJAX methods it provides, choose a method for your task that seems appropriate
  • build a request to your site that fetches the info and reacts on the response (shows a message box or updates some part of your page), wrap that in a function
  • make sure request errors do not go unnoticed by implementing an error handler
  • check out setInterval() to call that function you've just made repeatedly
  • final step: make sure that the interval will be stopped in case of an error condition (or provide a on/off button for the user, even) so the server is not hammered needlessly
Tomalak
Teach a man to code...
SeanJA
Simplest method is to use setInterval() to make ajax callback periodically.
HasanGursoy
A: 

There is a technique called Comet where-by your client-side script would instantiate a HTTP request which remains open for a long time. The server can then push data into the response as they happen. It's a technique to deliver a push notification.

The Wikipedia link has more information on real-world implementations.

abrereton
A: 

Instead of polling the server with AJAX calls you could also use push technology (COMET). This way you can push the results to the client(s) as soon as the server is done with it's work. There are many frameworks available like:

  1. JQuery plugin
  2. Cometd
  3. Atmosphere (if your on java)
Albert