Cover by Pedro Teixeira

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 9

Reading and Writing Streams of Data

WHAT’S IN THIS CHAPTER?

  • 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.

USING A READABLE STREAM

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

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required