rpc streams with client/server or between peers (both sides act as client & server)
Transport-agnostic RPC libraries utilizing node streams have already become quite popular. In my opinion, there is still a problem, though: What if I need to call methods from both ends? Create tow tcp servers? Ugly.
This little library provides RPC streams that you can use to do traditional client/server or to communicate between peers (both sides act as client & server).
var rpc = require'p2p-rpc-stream'net = require'net'// This node both// a server ...var server = rpccreateServerreplynull msgreplynull 'hello.'netcreateServervar rpc_conn = servercreateStream// ... and a client// at the same time!var client = rpccreateClientserverid // we need to know our own serversockpipeclient // send requests into the other direction and check for repliespiperpc_conn // check for requests and reply to thempipesockclientrequest'hello'ifer throw er // will return a Timeout error after some 5secsconsole.logreslisten3000
var rpc = require'p2p-rpc-stream'net = require'net'var client = rpccreateClientvar sock = netconnectport: 3000clientpipesockpipeclient
npm install p2p-rpc-stream
- allow custom request timeouts
(c) 2013 by Marcel Klehr