Chapter 9

Reading and Writing Streams of Data


  • Introducing the streams interface
  • Getting data from a readable stream
  • Controlling the flow of a readable stream
  • Writing to a writable stream
  • Solving the slow client problem
  • Using pipe to stitch streams together

Node has a useful abstraction: streams. More specifically, two very useful abstractions: readable streams and writable streams. They are implemented throughout several Node objects, and they represent inbound (readable stream) or outbound (writable stream) flows of data. An example of a stream is a TCP socket that you read from and write to, or a file that you append or read from sequentially. You have already come across some of them, but this chapter introduces them in a more formal and abstract way, paving the way for some more concrete examples.

NOTE A stream is an abstract construct that is implemented by several Node objects. The way you create or obtain a stream depends on the type of stream you are using. For instance, you can explicitly create a read or write stream based on a file, but a server-side TCP socket is a stream that is handed to you when a client connects.
Besides having the characteristics of a readable or a writable stream, an object can contain other properties or behaviors that are particular to it. For instance, a file readable stream also contains a “path” property that may not be present in other streams.


A readable stream is like a data faucet. ...

Get Professional Node.js: Building Javascript Based Scalable Software now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.