In a Node web server I want to flush HTML content at specific points as follows:

  • 1st chunk: <html><head> ... </head>
  • 2nd chunk: <body> ... </body>
  • 3rd chunk: </html>

e.g.:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});

  res.write('<html><head> ... </head>');                                           

  setTimeout(function() {                                                          
    res.write('<body> ... </body>');                                               

    setTimeout(function() {                                                        
      res.end('</html>');                                                          
    }, 2000);                                                                      

  }, 2000);

}).listen(8000);

The code above responds <html><head> ... </head><body> ... </body></html> after ~4s in a single chunk, however I noticed chunks should be >= 4096bytes in order to get flushed immediately:

var http = require('http');

http.createServer(function (req, res) {                                            
  res.writeHead(200, {'Content-Type': 'text/plain'});                              

  res.write(Array(4097).join('*'));

  setTimeout(function() {                                                          
    res.write(Array(4097).join('#'));

    setTimeout(function() {                                                        
      res.end('done!');                                                            
    }, 2000);

  }, 2000);

}).listen(8000);

The response from code above also takes ~4s but chunks are flushed immediately. I could pad small chunks to fill at least 4096bytes, just wondering if there's another "non-hacky" way.

In PHP this could be achieved through flush()/ob_flush() and disabling output_buffering

FWIW, I'm building a web server tool to experiment several configurations of HTML chunk output with a given delay between them in order to analyze how modern browsers handle it and pick the optimal configuration.

Thanks

有帮助吗?

解决方案

This is a semi-duplicate this question.

The answer is that is already does what you want, it's just that the browser doesn't parse and display anything until it has received enough data to bother parsing it. Sending your chunks of 4097 gives the browser a push to parse part of the document, it doesn't push Node to send the chunks differently.

You can test this out using curl if you put it in non-buffering mode.

curl -N localhost:8000
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top