title: How do I flush to event stream, when res.flush() does not work? (node.js)
body: <p>I use express and node.js</p>
  <p>I have been working a long time on getting a loading bar to work, and I finally figured out to use res.flush() since res.write() does not work due to compression middle-ware (to my understanding).</p>
  <p>Now when I uploaded it to the server I run into the same problem. The stream does not flush, and the loading bar only gets updated on res.end(). All the other progress that was supposed to be sent continuously is also sent on res.end() (all in one go).
  Since the res.flush() is not working like it is on my pc, I am out of ideas on what to do, since I can't debug on the server.</p>
  <p>I have an EventSource on the client side listening for updates to make to the loading bar, but on the live server the only update is when res.end() is called.</p>
  <pre><code>router.get('/statistics/:query/', function(req, res) {
      let query = req.params.query;
  
      res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
      })
      
      //I have absolutely no idea if this is how to transfer progress between two classes
      //If this can be improved please let me know
      //The goal is to send updates back to the router, while the static class is querying the database    
  
      let progressObject : any = new Object();
      progressObject.progress = 0;
      
      progressObject.update = function(newProgress) { 
          //The update function is called within the static class &quot;Statistics&quot; as database query is handled
      
          let isDone = Math.round(newProgress) == 100 ? true : null; //sets isDone to true if progress=100
          this.progress = newProgress;
          res.write(&quot;data: &quot;+JSON.stringify(this)+&quot;\n\n&quot;); //This is the real problem! It works on my PC
          res.flush(); //But on the server it is only sent on res.end() in the bottom
      }
  
      let barChartLabels = Statistics.getBarChartLabels(progressObject, query);
  
      barChartLabels.then(labels =&gt;{
          let responseObject = {
              message : labels,
              progress : null,
              chartTitle : &quot;Stats&quot;, 
              success : true,
          };
          res.write(&quot;data: &quot;+JSON.stringify(responseObject)+&quot;\n\n&quot;);
          res.end() //All progress data is only flushed here.
      }).catch(err=&gt;{
          console.log(&quot;error!: &quot;+err)
          let responseObject = {
              message : &quot;Something went wrong when querying the database&quot;,
              progress : null,
              success : false
          };
          res.write(&quot;data: &quot;+JSON.stringify(responseObject)+&quot;\n\n&quot;);
          res.end()
      })
  });
  
  </code></pre>
  <p>The most important thing is getting this to work on the server, but I am also very interested in knowing what a good approach for sending the loading progress to the router is.</p>