1

Hey guys, is there a more efficient way to do this?

setInterval('getAllWords()', 2000);

function getAllWords() {
    $.ajax({
        type: "GET",
        url: "queries.php",
        data: "action=all",
        success: function(data){
            dataArray = data.split('|');
            $('#words').html('');
            for (var i = 0; i < dataArray.length; i++) {
                if ((dataArray[i]).length > 1) {
                    $('ul').append('<li>' + dataArray[i] + '</li>');
                }
            }
        }
    });
}

ELABORATION (?)
This basically connect every 2 seconds to the database and checks for updated words, is there an easy way to implement a push request or something to minimise the database activity?

MORE INFO:
My hosting doesn't support installing of AJAX Push servers and they won't install one for me


:(

2

6 Answers 6

1

If you can't install a push server, consider this hack:

Run an infinite loop that only sends an answer to your ajax request once there's something new (while(true) { check for updated words}).

In your ajax code you can then simply start a request and as soon as it's finished start a new one (eg. readyState == 4 --> start new request)

2
  • oooh, I like this - is there any adverse affects I should be worried about though? Feb 9, 2011 at 17:24
  • your server will keep a socket open for as long as the script is running. if you get a lot of concurrent users your ports might get flooded and no new users could join - though if you're just starting this might be a minor concern.
    – jcfrei
    Feb 9, 2011 at 19:27
1

It sounds like you want server push

3
  • It does seem that way, I don't suppose you have some form of JavaScript/jQuery example? Feb 9, 2011 at 10:48
  • It's a non-trivial problem. I suggest starting with the External Links section at the bottom of the Wikipedia page for practical implementation advice.
    – Quentin
    Feb 9, 2011 at 10:50
  • Next question is: Is there anyway of doing that without having to install a server on the webserver? Feb 9, 2011 at 10:53
0

If you don't need internet explorer support, you could use webSockets

Otherwise there are several standard ways of implementing server push

Here is a way to implement long polling using php - use google to find more :)

0

If query caching is enabled on your db server, then this shouldn't be a cause for worry. The query cacher will not load the db until the words field is updated.

On other note,

I prefer using setTimeout than using setInterval. What if response from server takes more than 2 secs to return? setInterval will issue request again even if previous request is not complete.

So, I'd use this

function getAllWords() {

    $.ajax({
        type: "GET",
        url: "queries.php",
        data: "action=all",
        success: function(data){
            dataArray = data.split('|');
            $('#words').html('');
            for (var i = 0; i < dataArray.length; i++) {
                if ((dataArray[i]).length > 1) {
                    $('ul').append('<li>' + dataArray[i] + '</li>');
                }
            }

            setTimeout(getAllWords,2000);
        }
    });
}

getAllWords();
0

to minize the db activity, you could:

  • use a record to save whether something changed in your keywords instead of checking directly in the keywords'table.

OR

  • use Application scope variable : which do not exist natively in PHP. But if you have a APC installed on your server, you can use an APC variable.
-1

you can use setInterval() function, however efficiency of the method depend upon your requirement.

Please let me know your requirement.

2
  • Allow me to say why you were downvoted (even if I didn't vote you down) - It's because you haven't actually put an answer in the "answer box", you have just said what I said and then asked another question... Feb 9, 2011 at 10:46
  • oopss, I was just assumed that you have written setTimeout(),
    – Ankur
    Feb 9, 2011 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.