...

/

Piping Patterns: Combining Streams

Piping Patterns: Combining Streams

Learn how to combine streams using piping patterns.

As in real-life plumbing, Node.js streams can also be piped together by following different patterns. We can, in fact, merge the flow of two different streams into one, split the flow of one stream into two or more pipes, or redirect the flow based on a condition.

Combining streams

In this chapter, we have stressed the fact that streams provide a simple infrastructure to modularize and reuse our code, but there is one last piece missing in this puzzle: what if we want to modularize and reuse an entire pipeline? What if we want to combine multiple streams so that they look like one from the outside? The following illustration shows what this means:

Press + to interact
 Combining multiple streams into one
Combining multiple streams into one

From the above illustration, we should already get a hint of how this works:

  • When we write into the combined stream, we’re actually writing into the first stream of the pipeline.

  • When we read from the combined stream, we’re actually reading from the last stream of the pipeline.

A combined stream is usually a ...