https://github.com/programmarchy/node-json-rpc-stream
Streaming JSON RPC
https://github.com/programmarchy/node-json-rpc-stream
Last synced: about 2 months ago
JSON representation
Streaming JSON RPC
- Host: GitHub
- URL: https://github.com/programmarchy/node-json-rpc-stream
- Owner: programmarchy
- License: mit
- Created: 2015-04-03T20:08:51.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-06T23:04:16.000Z (about 11 years ago)
- Last Synced: 2025-12-28T04:31:54.988Z (6 months ago)
- Language: JavaScript
- Size: 164 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
json-rpc-stream
===============
Streaming JSON RPC.
how to use
----------
Create a simple server with a few methods:
```
var Source = {
fooSync: function () {
console.log('foo sync')
},
fooAsync: function () {
return function (cb) {
setTimeout(function() {
if (cb) {
cb(null, { boom: 'bang' })
}
}, 100)
}
},
barSync: function (lhs, rhs) {
return (lhs + rhs)
},
barAsync: function (lhs, rhs) {
return function (cb) {
setTimeout(function() {
if (cb) {
cb(null, (lhs + rhs))
}
}, 100)
}
}
}
var server = new RPCServer()
server.implementSync('fooSync', Source.fooSync)
server.implementAsync('fooAsync', Source.fooAsync)
server.implementSync('barSync', Source.barSync)
server.implementAsync('barAsync', Source.barAsync)
```
Then create a client and pipe it to the server:
```
var client = new RPCClient()
client.pipe(server).pipe(client)
```
To call methods on the client, use `client.callMethod(method, callback, arg1, arg2, ..., argN)` like so:
```
// calls a sync method with no arguments and no result
client.callMethod('fooSync', function (err) {
console.log('hello world')
})
// calls an async method with no arguments and a result
client.callMethod('fooAsync', function (err, result) {
console.log(result.boom) // prints 'bang'
})
// calls a sync method with arguments and a result
client.callMethod('barSync', function (err, result) {
console.log('result is', result) // prints 777
}, 770, 7)
// calls an async method with arguments and a result
client.callMethod('barAsync', function (err, result) {
console.log('result is', result) // prints 42
}, 40, 2)
```
You can also bind and apply methods:
```
client.applyMethod('barAsync', function (err, result) {
console.log('result is', result) // prints 777
}, [770, 7])
client.bindMethod('barAsync', function (err, result) {
console.log('result is', result) // prints 123
})(23, 100)
```