0

I have been working with jquery/ajax requests. I have successfully got a ajax request which will retrieve data from a database, the problem is, that i'm constantly serving window.setInterval() to refresh this function every x amount of seconds.

How would I change this to keep the ajax request alive, so it updates the html content without having to serve multiple requests to my ajax script.

My code follows:

window.setInterval(function()
{
  $(function () 
  {
    $.ajax({                                      
        url: 'Ajax.php'+SearchTerm, dataType: 'json',  success: function(rows)        
        {
        $('#NumberOfVotes').empty();

            for (var i in rows)
            {
                var row = rows[i];          
                var QuestionID = row[0];
                var Votes = row[1];
                $('#NumberOfVotes')
                .append(Votes);
            } 
        } 
    });       
  });
}, 500);
1
  • setInterval is the right way to do this. If you want an asynchronous server you should try node.js. Commented Mar 14, 2013 at 14:37

4 Answers 4

2

A lot of this depends on how your server would be able to update it's content dynamically. That said, what you are looking for is websockets. Websockets are designed to replace the long-polling paradigm.

EDIT: Since you use mainly php for your server technology, look at Ratchet. I've heard good things about it http://socketo.me/

Here is an excellent article on using websockets with HTML http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

.NET has a great socket library in SignalR http://signalr.net/

There is a myriad of php documentation on sockets out there http://php.net/manual/en/book.sockets.php

Sign up to request clarification or add additional context in comments.

6 Comments

A nice thing with SignalR is that it falls back to long-polling and similar techniques for older browsers that lack native support for web sockets.
@ChristoferEliasson Yes, amongst other things. The SignalR library is so revolutionary that Scott Hansleman and company have now included it as part of a standard web library for .NET. It's incredibly powerful
Should have specified that I am mainly using PHP
@user2146021 No worries. I've updated my answer with an excellent PHP websocket library that I've heard recommended quite often and retagged your question to make it a bit more clear.
The main issue with websockets is that they are still not well supported by some browsers
|
0

look into using web sockets - you could send the client a message anytime they need to go an look for new data - that way your not making any unnecessary requests. Try checking out pubnub -service is cheap and could handle everything you need.

Comments

0

You could set xhr.multipart = true and modify server code. see Multipart Responses Example Code. Alternative way is to use websockets as mentioned

Comments

0

You need something server side that keeps the request alive until it has something to return. This is usually called "Comet", "Long-polling" or "Push".

The principle is :

  1. You send a request client-side via AJAX
  2. Your server receives the request, and doesn't return a response yet. It sleeps/waits until it has something to return
  3. A new entry in your database ! Your server now has something to return : it returns some JSON data for the waiting request
  4. Your receive the response server side, display what you have to display, and go back to step 1 sending another request.

Now, the implementation server side will depend on the language/framework you are using.


Edit :

Some examples using PHP :

Comet and PHP

Simple Comet Implementation Using PHP and jQuery

1 Comment

I edited my answer adding examples of PHP Comet implementations. Hope this helps :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.