title: express.js not streaming chunked 'text/event-stream' resposne
body: <p>I'm trying to send a SSE <code>text/event-stream</code> response from an express.js end point. My route handler looks like:</p>
  
  <pre><code>function openSSE(req, res) {
    res.writeHead(200, {
      'Content-Type': 'text/event-stream; charset=UTF-8',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive',
      'Transfer-Encoding': 'chunked'
    });
  
    // support the polyfill
    if (req.headers['x-requested-with'] == 'XMLHttpRequest') {
      res.xhr = null;
    }
  
    res.write(':' + Array(2049).join('\t') + '\n'); //2kb padding for IE
    res.write('id: '+ lastID +'\n');
    res.write('retry: 2000\n');
    res.write('data: cool connection\n\n');
  
    console.log("connection added");
    connections.push(res);
  }
  </code></pre>
  
  <p>Later I then call:</p>
  
  <pre><code>function sendSSE(res, message){
      res.write(message);
      if (res.hasOwnProperty('xhr')) {
          clearTimeout(res.xhr);
          res.xhr = setTimeout(function () {
            res.end();
            removeConnection(res);
          }, 250);
      }
  }
  </code></pre>
  
  <p>My browser makes the and holds the request:
  <img src="https://i.stack.imgur.com/mR1ZZ.png" alt="enter image description here"></p>
  
  <p>None of the response gets pushed to the browser. None of my events are fired. If I kill the express.js server. The response is suddenly drained and every event hits the browser at once.
  <img src="https://i.stack.imgur.com/OiQMp.png" alt="enter image description here"></p>
  
  <p>If I update my code to add <code>res.end()</code> after the <code>res.write(message)</code> line It flushes the stream correctly however it then fallsback to event polling and dosen't stream the response.
  <img src="https://i.stack.imgur.com/cqmei.png" alt="enter image description here"></p>
  
  <p>I've tried adding padding to the head of the response like
  <code>res.write(':' + Array(2049).join('\t') + '\n');</code>
  as I've seen from other SO post that can trigger a browser to drain the response.</p>
  
  <p>I suspect this is an issue with express.js because I had been previously using this code with nodes native <code>http</code> server and it was working correctly. So I'm wondering if there is some way to bypass express's wrapping of the response object.</p>