Stream

  • What is stream in Node.js?
  • Why?
  • readable stream
  • writable stream
  • duplex stream
  • transform stream
  • pipe

What is stream in Node.js?

Put simply, stream is a pipeline to write and read chunked data using buffer. http module uses stream for a response which is writable stream.

https://medium.freecodecamp.org/node-js-streams-everything-you-need-to-know-c9141306be93

http://yosuke-furukawa.hatenablog.com/entry/2014/12/01/155303


Why?

It is useful to send data without allocating a lot of memory at once. Instead of waiting for loading huge file, it makes buffer and send the chunk of data.

Readable Stream

Readable stream is the stream to read data. Examples of Readable streams include:

Writable Stream

Readable stream is the stream to write data. Examples of Writable streams include:

Duplex Stream

Examples of Duplex streams include:

Duplex Stream

Duplex stream has feature both writable and readable stream.

Transform stream

Transform streams are Duplex streams where the output is in some way related to the input. Like all Duplex streams, Transform streams implement both the Readable and Writable interfaces.

pipe

pipe makes pipeline between readable stream and writable stream.

readableStream.pipe(writableStream)

How to use

This is simple example of readable stream and writable stream using http.

var http = require('http');
var fs = require('fs');
var path = require('path');

var app = http.createServer(function(req, res) {
  var readableStream = fs.createReadStream(path.resolve(__dirname, './test.txt'));
  readableStream.pipe(res);
});

response is writableStream by default so you can pipe it with readableStream.

options

property default default
encoding set encoding of strings null (raw buffer)
fd File descriptor null
bufferSize 64KB 64KB
start First byte to read null (whole)
end Last byte to read null (whole)

Difference between stream module and fs module

The stream module allows you to create an in memory stream object that isn't actually tied to a file. It is meant to be used as a shared interface for providing stream like access. Typically a module will push data from some source to be consumed as a stream from consumers outside the module.

Creating a stream from the fs module, on the other hand, generates a stream object attached to a file, which means that the fs module uses the file as the source of the streamed data.

stream-handbook

results matching ""

    No results matching ""