Package Exports
- rinvoke
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rinvoke) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
rinvoke
RPC library based on ZeroMQ.
Install
npm i rinvoke --save
Usage
Declare one or more function and run the server.
const server = require('rinvoke/server')()
// register a new function
server.register('cmd:concat', (a, b, reply) => reply(null, a + b))
// run the listener
server.run('tcp://127.0.0.1:3030', err => {
if (err) throw err
})
Then declare a client to call the function:
const client = require('rinvoke/client')()
// connect to the remote server
client.connect('tcp://127.0.0.1:3030')
// invoke the remote function
client.invoke('cmd:concat', 'a', 'b', (err, res) => {
if (err) console.log(err)
console.log('concat:', res)
})
Async await is supported as well!
const server = require('rinvoke/server')()
// register a new function
server.register('cmd:concat', async (a, b) => {
const val = await something()
return a + b
})
// run the listener
server.run('tcp://127.0.0.1:3030', err => {
if (err) throw err
})
API
Server
.register
: declare a new function, the internal routing is done by bloomrun, you can use the tinysonic syntax to declare new routes..run
: run the server on the specified url.parser
: use a custom parser, by default only strings are handled:
server.parser(JSON.parse)
.serializer
: use a custom serializer, by default only strings are handled:
server.serializer(JSON.stringify)
.errorSerializer
: use a custom serializer for errors, by default is handled with JSON.close
: close the server.use
: add an utility to the instance, it can be an object or a function, under the hood uses avvio.
server.use((instance, opts, next) => {
instance.concat = (a, b) => a + b
next()
})
server.register('cmd:concat', function (a, b, reply) {
reply(null, this.concat(a, b))
})
You can also use it to handle a connection with a database, listen for the 'close'
event if you need to shutdown the connection.
Client
.invoke
: invoke a remote function, pass the parameters as single values and a callback at the end..connect
: connect to a remote server.parser
: use a custom parser, by default only strings are handled:
server.parser(JSON.parse)
.serializer
: use a custom serializer, by default only strings are handled:
server.serializer(JSON.stringify)
.errorSerializer
: use a custom serializer for errors, by default is handled with JSON.close
: close the connection with the server
CLI
You can even run the server with the integrated cli!
In your package.json
add:
{
"scripts": {
"start": "rinvoke server.js"
}
}
And then create your server file:
module.exports = async () => 'hello!'
module.exports.key = 'hello'
key
is the key of your function.
You can also use an extended version of the above example:
function sayHello (rinvoke, opts, next) {
rinvoke
.serializer(JSON.stringify)
.parser(JSON.parse)
rinvoke.register('hello', reply => {
reply(null, { hello: 'world' })
})
next()
}
module.exports = sayHello
The options of the cli are:
--port # default 3000
--address # default 127.0.0.1
--protocol # default tcp
Acknowledgements
This project is kindly sponsored by LetzDoIt.
License
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
Copyright © 2017 Tomas Della Vedova