title: Simple long polling example with JavaScript and jQuery
body: <p>I'm trying to create a real-time website analytics dashboard which creates an open HTTP connection to the server using jQuery/JavaScript asynchronously to poll the server for updates to the data as and when they occur.</p>
  
  <p>The obvious start for this would be to use an <code>XMLHttpRequest</code> object or jQuery's <code>$.ajax</code> method to send a <code>GET</code> or <code>POST</code> request to the server asynchronously requesting some data. </p>
  
  <p>However, beyond sending one request at a time using a <code>setInterval</code> method every <code>30 seconds</code> I am not sure how to make the connection to the server persistent. Basically, I only want to send <strong>one http request</strong> and ensure the connection to the server stays open for polling!</p>
  
  <p>My example code with <code>setInterval</code> is as follows:</p>
  
  <pre><code>&lt;div id="analytics"&gt;&lt;/div&gt;
  &lt;script&gt;
  var analytics = document.getElementById('analytics');
  setInterval(function(){
      $.ajax({ url: "http://server.com/", success: function(data){
          analytics.innerHTML = data;
      }, dataType: "json"});
  }, 30000);
  &lt;/script&gt;
  </code></pre>