Chapter 5. Streams: Node’s most powerful and misunderstood feature

 

This chapter covers

  • What streams are and how to use them
  • How to use Node’s built-in streaming APIs
  • The stream API used in Node 0.8 and below
  • The stream primitive classes bundled since Node 0.10
  • Strategies for testing streams

Streams are an event-based API for managing and modeling data, and are wonderfully efficient. By leveraging EventEmitter and Node’s non-blocking I/O libraries, the stream module allows data to be dynamically processed when it’s available, and then released when it’s no longer needed.

The idea of a stream of data isn’t new, but it’s an important concept and integral to Node. After chapter 4, mastering streams is the next step on the path to becoming truly competent at Node development.

The stream core module provides abstract tools for building event-based stream classes. It’s likely that you’ll use modules that implement streams, rather than creating your own. But to exploit streams to their fullest, it’s important to understand how they really work. This chapter has been designed with that goal in mind: understanding streams, working with Node’s built-in streaming APIs, and finally creating and testing your own streams. Despite the conceptually abstract nature of the stream module, once you’ve mastered the major concepts, you’ll start to see uses for streams everywhere.

The next section provides a high-level overview of streams and addresses the two APIs that Node supports as of Node 0.10.

5.1. Introduction to streams

5.2. Built-in streams

5.3. Third-party modules and streams

5.4. Using the stream base classes

5.5. Advanced patterns and optimization

5.6. Summary

sitemap