Learn advanced node js, Piping streams

You may be thinking to yourself that streams seem fun, but there’s an awful lot of code that you have to use to implement a stream, and you have to worry about back pressure that seems a little complicated. Well I’m looking at our copy file that we created in the last lesson, and this is inside of the index.js file, found under chapter two, chapter two, lesson six, within the start folder. And what I’m going to say is, we can actually just delete all of these handlers, because we don’t need them. And I’m going to delete this reference to a high water mark, because we also don’t need it.

I can pipe a read stream to a write stream, simply by typing readStream.pipe(writeStream). So again, what we’re doing is the same thing. Instead of wiring up a bunch of listeners to listen for chunks of data and then pass those chunks of data into the write stream, the pipe method is doing it for us. The pipe method also automatically handles back pressure for us. The only thing that we didn’t do is wire up an air listener, so I can just do this on the end of a pipe method. So when any error occurs, we can handle it by logging it to the console.

So now we have the same program. I can come over here to the start folder and type node dot. And when I do a ls, you can see that we have our copy.mp4. So any read stream can be piped to any write stream. So we’ll take a look at a little sample of this. I’m going to get rid of our read stream here in the code, we don’t need it. I’m also going to change the write stream to write to something called file.txt, so we’re going to write to a text file. And where should we get the data from? Well let’s use process.stdin, so a standard input can pipe, remember, any read stream can pipe to any write stream.

So now, we’re going to go ahead and pipe data from standard input to a text file. So let me come over here to the terminal, and I will type node dot, and I don’t see anything happen right away, but if I type hello world, this data is actually being piped to a text file. Command + backslash will open up my menu here, and I can see there’s a file.txt. So the write stream’s still open, because process standard input is still open. That means I can pipe more text into, whoops, this writable.

Pretty cool, so I’m going to go ahead and close out this program now. And instead of creating my text file from typing data directly into standard input, what I’m going to go ahead and do is use a Unix pipe to actually pipe the data. So within my terminal, I can type echo hello world. So instead of repeating it back to us, what I want to do is, I want to take this data and send it into my program. And I can do so with a Unix pipe. And now I can say node dot, and what happens is, our program starts and finishes, and what we’ve done is, we’ve actually just piped that hello world directly into the file.

So something else that I can do is, instead of echoing the text to get it into the file using the Unix pipe, I can also just copy a file. So I’ll go ahead and cat ../ ../, so in the chapter two folder, I have a sample.txt file for you to use. And I can go ahead and pipe that into my program with a Unix pipe, using standard input. And when I run that, we can take a look at our file text, and you will notice that it has copied all of the text from the sample.txt file into our file.txt.

So the pipe function allows us to pipe data from any readable stream to any writable stream.

Leave a Reply

avatar
1000
  Subscribe  
Notify of