title: Implement long-polling API with Symfony
body: <p>I am trying to implement an API which uses the long-polling concept in Symfony framework.</p>
  
  <p>Let's say that I have a table 'feeds' which can only grow (assume that users can insert thier feed from other interface).
  I want to create a client-side real-time updated page. The idea is the following:</p>
  
  <ol>
  <li>Client send an ajax request with timestamp of last modification (first time sends 0)</li>
  <li>Server compares timestamp of client to timestamp, to retrieve all messages with bigger timestamp than the one sent by user</li>
  <li>If there are newer messages, return them immediately to the client, with the timestamp of the latest one
  On other hand, if there are no new messages, enter into a 2 minutes <strong>busy-wait loop</strong>, checking every 1-3 seconds (randomly) whether there are new messages.</li>
  <li>When client receive servers answer, browser updates view and <strong>immediately</strong> sends a new ajax request.</li>
  </ol>
  
  <p><strong>In other words, instead of send an AJAX call every x seconds, the server holds the request till it has new information for us.</strong></p>
  
  <p>Having good experience with Symfony I tried to implement a simple demo of this api, and it works great. I had a problem of session blocking (the ajax call is held so access to the server is not possible), so I simply added the following to the action:</p>
  
  <pre><code>public function executeIndex(sfWebRequest $request)
  {
    session_write_close();
         :
         :
  </code></pre>
  
  <p>(see also <a href="http://garethmccumskey.blogspot.com/2009/10/php-session-write-locking-and-how-to.html" rel="nofollow">this link</a>)</p>
  
  <p>Then I testes massive access to the API. 100 users works fine, 1000 everything crashes.
  I realized that I have two problems:</p>
  
  <ol>
  <li>For each access a new DB connection is opened</li>
  <li>For each access the server executes a new process</li>
  </ol>
  
  <p>For the first problem I tried to put <code>persistent: true</code> In my database.yml Doctrine connetor. When I monitored the server connections I saw that still each access to the API opens a new connection. So basically I am still blocked with the same two problems.</p>
  
  <p>Does anyone have any idea or experience with this issue?? Or maybe I should give-up the idea of implementing my api with Symfony??</p>