Check out a free preview of the full Networking and Streams course

The "Transform and Duplex Stream" Lesson is part of the full, Networking and Streams course featured in this preview video. Here's what you'd learn in this lesson:

James reviews the transform stream, which is a particular kind of duplex stream used to operate on a stream in transit. Then James looks at duplex streams, which can accept and deliver content at the same time.


Transcript from the "Transform and Duplex Stream" Lesson

>> One of the other things, one of the other kinds of streams, is the transform stream. Transform streams have all of the methods of readable and writable streams. So you can call .pipe on them, and you can pipe to them. And all of the events are there as well.

So there's the finished event, there's the readable event, end event, everything. And you place the transform stream between two other streams. And you can also chain transform streams in between a source and a sync. Transform streams, I think, are more intuitive than the next kind of stream, which is a duplex stream.

So duplex streams you can think of as, imagine if you had a separate readable stream and a separate writable stream, but they're just sort of glued together into the same stream instance. You can also sort of think of them like a telephone. Like with a telephone, you can talk whenever you want, the other person can listen to whatever they want, they could also talk whenever they want.

So both sides can be talking at the same time. And the flow of information is inherently bidirectional. And again, whenever you see something like this, a.pipe(stream).pipe(a), or whatever it looks like, this piping a stream into itself, there could even be a few steps in between. But when you basically have this kind of architecture, you're dealing with a duplex stream.

And some examples of duplex stream in Node core are things like if you create a server, like a TCP server with net.createServer, or if you create a TCP connection with net.connect, you get a duplex stream. So you can read and write to it, and so can the other side.

There are more examples of these in Node core, as well.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now