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

The "Readable 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 looks at readable stream methods, which are used when supplying data. Readable streams can also be paused and resumed.


Transcript from the "Readable Stream" Lesson

>> The next thing that we can look at is readable stream methods. readable stream methods are anything that you pipe from, so you call .pipe on the readable stream itself. And, Readable streams have an event called end that just like finish fires exactly once so you can listen for those with on or once, doesn't really matter.

And do something when the readable stream is done reading. Readable streams also have some other methods and events like the read method and the readable method, but you usually don't have to call these methods yourself and it's kinda error prone to do that yourself anyways. So I would kinda recommend letting modules and letting the pipe method take care of calling these things like .read and listening for the readable event.

But it's good to know that they're there in case you really have to dive into the weeds of how streams work. So we've seen some examples, but just to show you again how that works, you can call createReadStream to get a readable stream or also process.standardIn is a readable stream.

And then, instead of trying to call read yourself, which has kind of complicated semantics, it's best just to pipe it somewhere and I'm gonna cover some good modules for consuming readable streams in a bit. So we've already seen this kind of thing. Whoops. All right, you have to actually provide createReadStream with some kind of file name.

So I'll just read in the programs on source and printed out again. That's what a readable stream looks like. Two other kinds of streams and then I think we can take maybe a break. So the important thing to notice about readable streams is that there's this notion of paused mode and flowing mode.

So, by default, all readable streams are in paused mode. This means that they're only going to produce data when you're actually consuming data. So if you remember from the example we had a bit ago using concat-stream in an HTTP server, when we wanted to stop consuming data, all that we needed to do was to not call next.

And calling next inside of that transform function stopped reading from the readable stream. So the readable stream stopped producing data. This is the system that Node.js introduced with streams2 and what it gives you is this kind of automatic way of dealing with back pressure. So back pressure is important concept for Node.js streams.

You could think of it like, all right, so you've got a really nice server on a really fat pipe. And you can push terabytes maybe it's like 10 gigabit fiber, I don't know. So your server is really good and you're trying to push down a video file. But somebody is trying to watch that video from a phone on an edge connection on like 2G.

So it wouldn't really make sense to keep pushing data at that client because it can't read it very fast. So it's just gonna either drop packets or it's gonna lose that data. So what backpressure lets you do at the protocol level there's a lot happening in TCP land where with windowing, where the connection can sort of inform the consumer to stop producing, and so Node.js will provide a lot of this for you relatively automatically.

So, when your readable stream can know when it should sort of produce more data and when it shouldn't, and some of the pieces behind the scenes deal with this. The other way that readable streams works is called flowing mode. And this for compatibility reasons can still be invoked, and it's sometimes useful if you just wanna consume a stream really quick, in a kind of non-production setting just to see something or if you're gonna buffer up all of the data anyways.

It really doesn't matter and you can do it this way but generally speaking, you don't wanna invoke flowing mode unless you have a good reason. But you do that by calling .resume or if you listen for the data event, then it sort of kicks off this old node because streams v1 would only emit data and events and you had to listen for those.

So this is kind of just a hack for compatibility that is still around. You can still use it but generally you can avoid it.

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