I was using a piece of code similar to this until I discovered the 'setEncoding' method. While refactoring the code, this question came to me.
This comes into play when you're creating a new Readable Stream from scratch. A Readable Stream needs to begin somewhere and there needs to be a way to get data into that new Stream. When doing a
push() like in your question, you're buffering data until it is read at which point it is then flushed from the stream and then piped/read into the destination for consumption. The consumer will continue reading the data until 'null' is hit which indicates the stream is done outputting data.
var stream = require('stream'); var readable = new stream.Readable(); // new empty stream.Readable readable.push('some data'); readable.push(null); // Done writing data
To get the fullest picture on how streams work under the hood and the various types of streams, you should read the Stream Handbook by Substack. That should answer any lingering questions you might have.