MyGGaN
MyGGaN

Reputation: 1806

close long poll connection, jQuery-ajax

Background
I use a Tornado-like server with support for long-polls. Each new web page a user comes to sets up a long poll to the server like this:

$.ajax({
    type: 'GET',
    url: "/mylongpollurl/",
    dataType: 'application/json',
    success: function(json) {
        // I do stuff here
    },
    error: function(xhr, errText, ex) {
        // If timeout I send a new long-poll request
    }
});

Problem
I will now rely on data that I get from Fiddler monitoring all requests made from my browser (FF at the moment).

  1. Page 1 is loaded and the long-poll request is made, now idling at server side.
  2. I click a link to page 2 and that page is loaded and setting up a long poll request, BUT the long poll request from page 1 is still idling at server side (according to Fiddler).

This means that I will stack all long poll calls when clicking around the page, thus end up with lots of active connections on the server (or are they maybe sharing connection?)

My thoughts
- As it's a Tornado-like server (using epoll) it can handle quite a lot of connections. But this fact is not to exploit in my opinion. What I mean is that I prefer not to have a timeout on the server for this case (were the client disappears).
- I know those stand alone pages better uses a common head and only swap content via ajax calls but this design we use today was not my call...
- The best way to solve this would probably be to have the connection reused (hard to pull off I think) or closed as soon as the browser leaves the page (you click to another page).

Thanks
-- MyGGaN

Upvotes: 3

Views: 3143

Answers (1)

EricLaw
EricLaw

Reputation: 57075

For long-polling connections, you need to ensure that you have the "Streaming" option set inside Fiddler. Otherwise, Fiddler will keep the connection open waiting indefinitely for the response to finish.

Normally, when you navigate from page to page, the client should tear down the open long-poll connection, effectively closing the connection. I say should because this doesn't always work properly, for instance, when you close a popup window in IE.

Upvotes: 4

Related Questions