0

Assume I have a server support client side long polling. then my client-side code is like this:

var polling = function() {
    $.ajax({
        url: "/polling"
    }).done(function(data) {
        // polling again
        polling();
        // process the pushed data
        ...
    });
}
polling();

this should work when i wanna push something to the client while the client continuously polling to "/polling".

however, you may notice that there are "time gap" between the client received a pushed data and next polling reaches the server. data in this "time gap" would be lost.

there are kinds of server side workarounds to avoid this problem. but i want to know if there are any workarounds in the client-side? such as: * could the client keeps the long polling request always connected? i find Gmail should do some tricks like this. i chat in gtalk but don't see the "/bind" request interrupts. * should ajax receives in-complete data while transfering? then the connection can be connected forever. * should websocket works? if so, what can i do without HTML5?

marstone
  • 686
  • 1
  • 10
  • 23
  • um, why would you have a time gap? Shouldn't your session, request know when the last time information was returned? – epascarello Jan 28 '13 at 18:16
  • @epascarello, yes server knows when the last polling. however, when the server had something to push while in the gap of 2 pollings, the server should cache/persist the data for a while when the subsequent polling reaches. but if there is no gap, the server just do not need the cache. – marstone Jan 28 '13 at 18:22

1 Answers1

1

You might want to consider something like Pusher, it will make your life much easier when dealing with such things (because they give you a library to handle all of this in a nice package).

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43