Networking and Streams Duplex Streams
Transcript from the "Duplex Streams" Lesson
>> Sorry, so a would be like a readable or a transform. No, a it actually is a duplex.
>> It's gonna be an another duplex stream.
>> Both of these are duplex streams actually.
>> So a would pipe something into stream, which is duplex and then stream, also-
>> So maybe this is a good time for me to just write out an example of how we would use a duplex stream to make like a little echo server, cuz this is I think a good opportunity. All right, so I'll make a file called echo.js.
[00:00:37] And I'm gonna push these all in a moment so that you can play around. So if we require the net module, we can do net.createServer. The net.createServer function by default, accepts function argument that for every TCP connection that's incoming, returns a stream, and that stream is a duplex stream.
[00:01:05] And then you can call .listen on that server object. So here we're gonna create a TCP server on port 5000. And what we can do is, we can just, Pipe, Let me think. So we can pipe, Well, here's another thing that you can do with the duplex stream is you can pipe a stream into itself.
[00:01:33] Because the readable and writable side, it's not like a transform stream where it's doing a transformation. It's actually decoupled, so this will not create an infinite loop, it will actually just create an echo server. And then maybe we can, I'll create another stream where we can sort of do a proxy would be a good second step for this.
[00:01:54] And then it will look more like a duplex stream. Okay, so if I run this program and now if I do netcat to localhost 5000, I get back the same thing that I put in. This used to be the de facto example that Ryan Dahl would do, or other people would do to like show node js echo servers.
[00:02:21] Cuz echo servers are actually pretty hard to write in languages that don't have kind of a runtime that can handle asynchronous pulling in sockets and all that kind of stuff easily. So it's a pretty nice little thing to do. And the next thing that we could do is, let's make a little proxy now, an echo proxy.
[00:02:46] So we're gonna keep our echo server running on port 5000, but we're gonna run a proxy server on port 5001. That's going to forward incoming connections to our proxy server and then back out again. So I'll call this proxy.js. We can use the net module to make our TCP connections.
[00:03:04] And we're gonna have a server as well. So we need to create a server. We get a duplex stream and our server will listen on port 5001. But then every time we get a connection, we need to actually connect to the other server running on port 5000. So you can omit the localhost, but this just makes a bit more explicit.
[00:03:30] Now we can pipe the incoming data into that socket. And then the outgoing data can go back into the other end of the duplex stream. So this is more similar to the example that I put in the slides where you have a.pipeb.pipea again. So if I run our proxy server now, whoop, it's not on 5001, I must be peering something else.
[00:03:57] 5005, okay, so now if I connect to local host 5005, say hello. And it's actually going to the proxy server and then the proxy server is going to the echo server and then back again. Cool, hopefully this is a more concrete example of what is meant by the duplex stream.
[00:04:25] I think it mostly takes just playing around with this kind of stuff and then you sort of build up an intuition for how you can use these kinds of abstractions in your own programs.