5

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

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Looks like curl by default bufferizes chunks of at least 4k, on the other hand Chrome requires the 1st chunk to be at least 1k then upcoming chunks can be smaller than that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.