title: What are the ways to display 'chunked' responses as soon as they come into AngularJS?
body: <p>Currently I have a problem displaying 'chunks' of responses that I am sending from my Web Service Node.js server (localhost:3000) to a simulated client running on a Node.js server (localhost:3001).</p>
  
  <ul>
  <li>edit * - Current implementation just uses Angular's %http as the transport without web-sockets</li>
  </ul>
  
  <p>The logic goes as follows:
  <br><br>
  1 . Create an array on the client side of 'Cities' and POST them (from the AngularJS controller) to the Web Service located at: localhost:3000/getMatrix</p>
  
  <pre><code>$http({
      method: 'POST',
      url: 'http://localhost:3000/getMatrix',
      data: cityArray
  }).
  success(function (data,status,headers,config){
      // binding of $scope variables
  
      // calling a local MongoDB to store the each data item received
      for(var key in data){
          $http.post('/saveRoutes', data[key])
          .success(function (data, status){
              // Data stored
          })
          .error(function (data, status){
              // error prints in console
          });
     }
  
  }).
  error(function (data,status,headers,config){
      alert("Something went wrong!!");
  });
  </code></pre>
  
  <p><br></p>
  
  <p>2 . The Web Service then runs through its process to make a matrix of 'Cities' (eg. If it was passed 5 cities, it would return a JSON matrix of 5by5 [25 items]).  But the catch is that it passes back the data in 'chunks' thanks to Node's > response.write( <em>data</em> )</p>
  
  <p>Side note - Node.js automatically sets 'Transfer-Encoding':'chunked' in the header</p>
  
  <pre><code>* Other code before (routing/variable creation/etc.) *
  
  res.set({
       'Content-Type':'application/json; charset=utf-8',
  });
  res.write("[\n");
  
  * Other code to process loops and pass arguments *
  
  // query.findOne to MongoDB and if there are no errors
  res.write(JSON.stringify(docs)+",\n");
  
  
  * insert more code to run loops to write more chunks *
  
  // at the end of all loops
  res.end("]");
  
  // Final JSON looks like such
  [
      { *data* : *data* },
      { *data* : *data* },
      ......
      { *data* : *data* }
  ]
  </code></pre>
  
  <p><br></p>
  
  <p>Currently the problem is <strong>not</strong> that the 'chunked' response is not reaching its destination, but that I do not know of a way to start processing the data as soon as the chunks come in.</p>
  
  <p>This is a problem since I am trying to do a matrix of 250x250 and waiting for the full response overloads Angular's ability to display the results as it tries to do it all at once (thus blowing up the page).  </p>
  
  <p>This is also a problem since I am trying to save the response to MongoDB and it can only handle a certain size of data before it is 'too large' for MongoDB to process.</p>
  
  <p>I have tried looking into Angular's <a href="http://docs.angularjs.org/api/ng/service/$q" rel="nofollow noreferrer"><strong>$q</strong> and the <strong>promise/defer API</strong></a>, but I am a bit confused on how to implement it and have not found a way to start processing data chunks as they come in.</p>
  
  <p><a href="https://stackoverflow.com/questions/13169617/process-chunked-response-with-angularjs-http"><strong>This question</strong></a> on SO about dealing with chunks did not seem to help much either.</p>
  
  <p>Any help or tips on trying to display chunked data as it comes back to AngularJS would be greatly appreciated.</p>
  
  <p>If the responses could be informative code snippets demonstrating the technique, I would greatly appreciate it since seeing an example helps me learn more than a 'text' description.</p>
  
  <p>-- Thanks</p>