p2p-rpc-stream

rpc streams with client/server or between peers (both sides act as client & server)

p2p-rpc-stream

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 = rpc.createServer({
  echofunction(msgreply) {
    reply(null, msg)
  },
  hellofunction(reply) {
    reply(null, 'hello.')
  }
})
 
net.createServer(function(sock) {
  var rpc_conn = server.createStream()
  
  // ... and a client 
  // at the same time! 
  var client = rpc.createClient(server.id) // we need to know our own server 
  
  sock
    .pipe(client) // send requests into the other direction and check for replies 
    .pipe(rpc_conn) // check for requests and reply to them 
    .pipe(sock)
  
  client.request('hello', function(erres) {
    if(er) throw er // will return a Timeout error after some 5secs 
    console.log(res)
  })
}).listen(3000)
var rpc = require('p2p-rpc-stream')
  , net = require('net')
 
var client = rpc.createClient()
 
var sock = net.connect({port: 3000}, function(sock) {
  client.pipe(sock).pipe(client)
})

npm install p2p-rpc-stream

  • allow custom request timeouts

(c) 2013 by Marcel Klehr
MIT License