Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - Why bother with while loop when reading data off readable stream within the .on('readable') event

Tags:

stream

node.js

What is the advantage of this way of reading data off a readable stream such as a request:

request.on('readable', function(){
    var chunk = null;
    while (null !== (chunk = request.read())) {
        response.write(chunk);
    };
});

vs this way without a while loop inside? Since 'readable' will just keep firing why bother with the while loop?

request.on('readable', function(){
    var chunk = request.read();
    if(chunk !== null){
        response.write(chunk);          
    }
});
like image 233
HelpMeStackOverflowMyOnlyHope Avatar asked Jul 30 '15 01:07

HelpMeStackOverflowMyOnlyHope


People also ask

What will happen if we put a readable stream into the flowing mode while there are no consumers connected to it?

In the flowing mode, data can actually be lost if no consumers are available to handle it. This is why, when we have a readable stream in flowing mode, we need a data event handler.

How do you read from a readable stream in node JS?

read() Method. The readable. read() method is an inbuilt application programming interface of Stream module which is used to read the data out of the internal buffer. It returns data as a buffer object if no encoding is being specified or if the stream is working in object mode.

What is the main benefit of using streams over regular read methods to work with a files data?

The good thing about Streams is how they help read large chunks of data in pieces and process them instead of reading them all at once which is the traditional way. This helps when you have limited memory space. Working with streams can be as simple as reading content from a text file using the fs (file system) module.

Which of the following is true about a readable stream?

Q 4 - Which of the following is true about readable stream? A - Readable stream is used for read operation.


1 Answers

As per the API documentation:

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

Using the res.on('data') event you get the data when it's ready. This will allow your program to move along and do other things until the next chunk of data is ready to be processed (remember HTTP is over TCP which comes in chunks).

Using the below code might work, but why do that when it's needlessly eating CPU cycles and blocking other code from executing (remember that your Node.js JavaScript code is single-threaded). Using events is far better since it allows your JavaScript to run and process input/output without blocking the process needlessly.

request.on('readable', function(){
    var chunk = null;
    while (null !== (chunk = request.read())) {
        response.write(chunk);
    };
});
like image 107
Evan Shortiss Avatar answered Oct 17 '22 02:10

Evan Shortiss