multiplex-rpc-streams
v1.2.0
Published
RPC with streams using multiplex
Downloads
2
Readme
multiplex-rpc-streams
RPC with streams using multiplex
Example
var rpc = require('multiplex-rpc-streams')
var server = rpc({
range: function(start, end, stream) {
var i = start
while (i <= end) {
stream.write(String(i++))
}
stream.end()
},
uppercase: function(str, stream) {
stream.write(str.toUpperCase())
stream.end()
}
})
var client = rpc.client(['range', 'uppercase'])
client.pipe(server).pipe(client)
client.uppercase('crack! zlopp! urkk! biff! clank-est!')
.pipe(process.stdout)
client.range(10, 20)
.pipe(process.stdout)
Usage
rpc(methods={})
Each method can accept any number of expected arguments understanding that the last argument will always be a stream to respond to. This is a normal node stream that can be written or piped to.
var server = rpc({
fetchActivity: function(from, to, stream) {
db.activityStream(from, to)
.pipe(stream)
}
})
rpc.client(methodNames=[])
Provide an array of method names which will be exposed as methods on the returned client
.
var client = rpc.client(['fetchActivity'])
client.fetchActivity('2014-11-07', '2014-12-07')
.pipe(process.stdout)
You can also provide the method names as individual String
arguments
var client = rpc.client('range', 'uppercase')
Connecting the streams
The return value of rpc()
and rcp.client()
are both duplex streams which can be piped together to make the magic happen. See substack/stream-handbook#duplex and this rant by dominictarr for further information.
client.pipe(server).pipe(client)
To expose these streams over the network you can use the listen
and connect
methods. Both accept port/host/path/callback arguments. See parse-connection-args for documentation.
var server = rpc({
whisper: function(str, stream) {
stream.write(str.toLowerCase())
stream.end()
}
})
server.listen(4000)
var client = rpc.client('whisper')
client.connect(4000, function() {
console.log('RPC client is now connected to port 4000')
})
License
MIT