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

The "duplexify" 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 introduces a duplexify, which creates a writeable and readable stream into a single streams2 duplex stream.


Transcript from the "duplexify" Lesson

>> So now we get into some really fun stuff. This one is called duplexify. So duplexify lets you define a stream, where maybe you have to do some setup first before you can really get a handle. So as an example, good thing that you can use duplexify for is maybe you have to do some setup and then you're gonna write to a file.

So maybe you have to make a directory first, and then you have to write input into that directory. So duplexify, I can show you an example for how to do that. It makes setting that up a lot easier. And then you don't have to expose an asynchronous interface like with a callback.

You can just expose something that people can write to right away. Those writes get buffered and it gets put into the file. So let's go ahead and do that with duplexify, call this one dup.js. So we require duplexify, I think I have that installed. Just in case I don't, require('duplexify').

And now maybe we have. So this is gonna be a module, suppose, that is gonna be a logger, so, It'll take a log name and we're gonna return a stream that consumers can write their log data to in a log directory. So we can use this package called mkdirp, which you can install with npm install mkdirp, like that, do it the faster way, and, This is like the Unix command mkdir -p.

So if the directory already exists, that's fine. If it doesn't exist, then it creates it. So we're gonna make a directory called logs. And then inside of that directory, we're gonna, We're gonna make a new log file that we'll write to. So to do that, we can return the duplexify stream.

And then later, once we've done something asynchronous, there could be even more steps involved, we're gonna call fs.createWriteStream. So we'll do fs.createWriteStream. And it'll be logs/the name.log, why not? So we couldn't do this if the directory called logs didn't exist. So this module, duplexify, sort of lets us defer some of those interfaces that we don't, so that we don't have to write our own buffering, queueing logic.

It's pretty useful sometimes. So this is a writable stream that we can now set into our duplexify stream. So you call d.setWritable(w), which is gonna be that stream. And duplexify will also forward the errors. So if there's an error event on the w instance, which is an event, you have to listen for normally.

Then duplexify will sort of take that in and put it out the other side. So someone who's consuming it can use it. So gotta move this example into the directory. So we'll call this file our api.js, and then we'll make our script that's gonna run this file. So we'll load our duplexified API.

So this is like a logger script, so we could call it, whoops, log. So it's gonna give us a stream back, a writable stream. So I don't know, we'll just stream.write, ( plus new line. And maybe we'll do that a couple of times, and then we'll quit. So just do it every 100 milliseconds until, 5 times.

And then we'll close the stream. Okay, so we're just using our interface that's provided by duplexify. Notice how there's no log directory. Now when we run this script, there is a log directory. And inside of that log directory, we didn't pass a name in, so we got undefined.log, but that's okay.

So there we go, some data. That's about what I would expect it to do, so good. I'll just commit these.

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