Transcript from the "rpc-stream" Lesson
>> Okay. So next stop, we can do with another fan user online package. This one is gonna, be useful for the next two examples, so its called rpc-stream. The rpc-stream, Read me, is really worth reading for some sort of like some ideology about how you should try to make your your interfaces as general as possible so that you get the flexibility of, for example, what we did with the VPN example.
[00:00:27] If that network connection had baked in its own crypto, we couldn't have done any of that, or if it had been hardwired to a particular interface. I recommend you give that a read, I can just give you an example. It's like a nice rant about how you can build little pipelines like that, it's great.
[00:00:46] rpc-stream lets you call methods on a remote service. So it's rpc remote procedure call, and you can pack it into a stream. So, this isn't always the best idea, but sometimes it's convenient enough way to do things, it works based on defining methods. And the methods have callbacks, and then you need to call the callback to inform the remote end that everything is okay.
[00:01:15] And this all works in a stream, so here I've got a server that's set up with a duplex stream connection. If we run this, anytime a client connects that can speak the rpc-stream protocol, they can call a method called hello with a name, and then the server responds back prepending a string to the response.
[00:01:35] So how do you in this case with the name that they provide? Here's a client for that, so, I'm just gonna go ahead and copy paste these because they're kind of hard to write freehand. I usually mess them up. So make a directory rpc, [SOUND] and write that out to server.js, and then write this one out to client.js, So if we go ahead and look at the server, could just step through what's happening here.
[00:02:11] So we've got our TCP server, stream is a duplex stream, inside of that function. And then we pipe stream into rpc, which also returns a duplex stream. So yet again, we have a.pipe.b.pipe a. So it's common pattern with this kind of stuff. Then we've got the rpc internals, like defining some methods.
[00:02:35] And that's it. We're listening on a port. The client, meanwhile, does a similar kind of thing. a.pipe b.pipe a again, by the way, except this is the client side of a TCP connection. So we're doing client that pipe, whatever. And then, we need to list out the methods that the remote has, and we can then call those methods.
[00:03:03] So here I'm using process.env.USER, which is just like the USER, in your system.
>> Read me, and print out the results, and then we close the connection. So, now if I run the server, and I run the client, I get howdy substack. That's exactly what I would expect.
[00:03:25] So, rpc-streams are like sometimes useful, sometimes you just have something that sort of just some methods that you wanna expose, and you don't wanna make your own hardwired wire protocol. You don't wanna have to like sit down and come up with like a JSON, like new line delimited schema for doing this stuff, and like having the sequence numbers on every method, so that things get routed to the correct place.
[00:03:52] So these kinds of tools are sometimes handy for this sort of thing.