304 response with long-polling issue

127 views
Skip to first unread message

dan

unread,
Dec 14, 2011, 8:44:31 PM12/14/11
to nginxpushstream
Hi,
I'm using long-polling and making an ajax call to subscribe to
channel. I've noticed that if I dont post any messages, I get a 304
back from the server, which is expected. What I usually do is
reconnect using an event handler in jquery which picks up on the
response (304 or 200). The problem is that the 304 response headers
come, and the body then takes another 5min to complete (even though
its 0 bytes). Only once the response is complete (body+header) will
the event handler kick in. I've adjusted the following:

push_stream_longpolling_connection_ttl 5s;
push_stream_subscriber_connection_ttl 5s;

But It has had no effect thus far. I know that
push_stream_longpolling seems to be working as expected, because the
http response is received after 5 seconds of no activity, but the
response still takes 5 min to download the body after the header is
received. Thanks for any help in advance.
-Dan

Wandenberg Peixoto

unread,
Dec 15, 2011, 9:01:46 AM12/15/11
to nginxpu...@googlegroups.com
Hi,

could you send the snippet of jquery code, to check if the problem is at server or client side?

Regards,
Wandenberg

dan

unread,
Dec 15, 2011, 1:58:51 PM12/15/11
to nginxpushstream
Here is the jquery I'm using:

function listen(cid) {
$.ajax({
url: "http://foo.com/lp/"+cid,
dataType: 'jsonp',
jsonp:false,
jsonpCallback: "processMessage"
})
.fail(function(){
//catch 304, 404, 500, etc. Jquery considers a 304 a failure
listen(cid);
});
}

function processMessage(data){
var id = data[0].id;
var channel = data[0].channel;
var message_data = jQuery.parseJSON(data[0].message);

//listen again
listen(channel);
}

The key is that jquery seems to interpret a 304 as a failure.
-Dan

Wandenberg Peixoto

unread,
Dec 15, 2011, 10:42:21 PM12/15/11
to nginxpu...@googlegroups.com
Hi Daniel,

I made some changes on your code. I used jQuery 1.7.1 and Firefox 8.

Try to use this code if you have some problem tell me (specifying browser and jQuery versions).

Regards,
Wandenberg

      var lastModified = null;
      var etag = 0;
      var listenRef = null;

      function callListen(cid) {
        if (listenRef) {
          clearTimeout(listenRef);
        }

        listenRef = setTimeout(function(){listen(cid);}, 100);
      }

      function getHeaders(xhr) {
        if (xhr && xhr.getResponseHeader && xhr.getResponseHeader('Last-Modified')) {
          etag = xhr.getResponseHeader('Etag');
          lastModified = xhr.getResponseHeader('Last-Modified');
          return true;
        }
        return false;
      }

      function listen(cid) {
        $.ajax({
          url: "http://localhost:9080/lp/"+cid,

          dataType: 'jsonp',
          jsonp:false,
          jsonpCallback: "processMessage",
          beforeSend: function(xhr) {
            xhr.setRequestHeader("If-None-Match", etag);
            xhr.setRequestHeader("If-Modified-Since", lastModified);
          }
        })
        .always(function(data, textStatus, xhr){
          //console.log('success');
          if (getHeaders(xhr)) {
            callListen(cid);
          };
        })
        .fail(function(xhr, textStatus) {

          //catch 304, 404, 500, etc.  Jquery considers a 304 a failure
          //console.log('failed');

        });
      }


      function processMessage(data){
        var id = data[0].id;
        var channel = data[0].channel;
        var message_data = jQuery.parseJSON(data[0].message);

        //process message
        //console.log(message_data);
        var html = $('BODY').html() || "";
        $('BODY').html(html + " <br> " + data[0].message.msg);

        //listen again
        //callListen(channel);
      }

      $(document).ready(function() {
        callListen("ch1");
      });
Reply all
Reply to author
Forward
0 new messages