Build your distributed functions system with Rinvoke! Rinvoke is a RPC library based on net sockets, can work both with tcp sockets and ipc.
It has built in reconnect logic and supports multiple parser/serializers, such as msgpack or protbuf.
Internally uses tentacoli to multiplex the requests and avvio to guarantee the asynchronous bootstrap of the application, it also provide an handy request validation out of the box with .
Install
npm i rinvoke --save
Usage
Rinvoke could be used as a server or client, so when your require it you must specify it.
Let's see an example for the server:
The client could seem synchronous but internally everything is handled asynchronously with events.
Checkout the examples folder if you want to see more examples!
API
Server
server([opts])
Instance a new server, the options object can accept a custom parser/serializer via the codec field.
Registers a new procedure, the name of the procedure must be a string, the function has the following signature: (request, reply) where request is the request object and reply a function t send the response back to the client.
Rinvoke offers you out of the box a nice and standard way to validate your requests, JSON schema!
Internally uses ajv to achieve the maximum speed and correctness.
Run the server over the specified port (and address, default to 127.0.0.1), if you specify a path (as a string)
it will use the system socket to perform ipc.
rinvoke.listen(3000, err => { if (err) throw err})rinvoke.listen(3000, '127.0.0.1', err => { if (err) throw err})rinvoke.listen('/tmp/socket.sock', err => { if (err) throw err})
Client
client(options)
Instance a new client, the options object must contain a port or path field, furthermore can accept a custom parser/serializer via the codec field. If you want to activate the automatic reconnection handling pass reconnect: true (3 attempts with 1s timeout), if you want to configure the timeout handling pass an object like the following:
Invoke a procedure on the server, the request object must contain the key procedure with the name of the function to call.
The callback is a function with the following signature: (error, response).
Fire (and forget) a procedure on the server, the request object must contain the key procedure with the name of the function to call.
The optional callback will be called if there is an error while sending the message, or after the message has been sent successfully.
The callback is a function witb the following signature: instance, options, next.
Where instance is the client instance, options, is an options object and next a function you must call when your code is ready.
This api is useful if you need to load an utility, a database connection for example. use will guarantee the load order an that your client/server will boot up once every use has completed.
rinvoke.use((instance, opts, next) => { dbClient.connect(opts.url, (err, conn) => { instance.db = conn // now you can access in your function the database connection with `this.db` next() })})
onClose(callback)
Hook that will be called once you fire the close callback.
rinvoke.onClose((instance, done) => { // do something done()})
close(callback)
Once you call this function the socket server and client will close and all the registered functions with onClose will be called.
rinvoke.close((err, instance, done) => { // do something done()})
CLI
You can even run the server with the integrated cli!
In your package.json add: