hydra-plugin-rpc
v0.0.4
Published
simple distributed RPC using Hydra
Downloads
22
Readme
Hydra-plugin-RPC
Create and consume remote procedure calls in hydra with ease.
Install
$ npm i --save hydra-plugin-rpc
Use
Two methods are added to the hydra instance to add or consume methods
// hydra.methods(obj: Object);
// Object has keys of method names and values of functions.
hydra.methods({
methodName: (arg1, arg2, arg3) => arg1 + arg2; // return value or Promise
});
// hydra.call(methodName: String, ...arguments) => Promise
// First argument is method name.
// Remaining arguments are sent to method (must be serializable).
// Returns a promise with the result.
hydra.call('methodName', arg1, arg2, arg3).then(...);
Example
// Service1.js
const hydra = require('hydra');
const HydraRPC = require('hydra-plugin-rpc');
const Promise = require('bluebird');
hydra.use(new HydraRPC());
hydra.init({...}).then(() => {
hydra.methods({
ping: () => 'pong',
sleepPing: delay => Promise.resolve('pong').delay(delay) // optionally return promises
});
});
// Service2.js (even works on separate machines!)
const hydra = require('hydra');
const HydraRPC = require('hydra-plugin-rpc');
hydra.use(new HydraRPC());
hydra.init({...}).then(() => {
hydra.call('ping').then(result => console.log(result)); // Logs "pong"!
hydra.call('sleepPing', 1000).then(result => console.log(result)); // Result comes back after 1000 ms!
});