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