I have started learning streaming APIs and I found one of the good documentations here. There was a comparison given by author to demonstrate the effectiveness of the streams.
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
fs.readFile(__dirname + '/data.txt', function (err, data) {
res.end(data);
});
});
server.listen(8000);
It is told that in the example above, for every request the whole file would be read and stored in memory which may create problems for large number of concurrent connections. Sounds good!
Now, for the solution in the second example:
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
var stream = fs.createReadStream(__dirname + '/data.txt');
stream.pipe(res);
});
server.listen(8000);
it is told that since res
is itself is a stream hence we can read the file via stream and pipe the result into res which would not create the memory issue.
Question
Will the browser would keep the connection open util the whole file is read since according to me the browser knows about HTTP
only hence how would it handle the stream scenario. Also, wouldn't it take longer to stream the file vs sending whole file at a time?