JSON-RPC server over WebSockets
JSON-RPC 2.0 server over WebSockets.
For a JSON-RPC 2.0 client check out CyPRus.
UPDATE (2018-15-02): Deprecated in favour of Jayson.
npm install mhingston/respect
// Import the module
const Respect = require('respect');
// Declare an object literal with all the methods you want to expose...
const methods =
{
foo: () => 'hello',
bar: (a, b) => a + b,
baz: ({name}) => 'hello ' + name
};
// ...or pass in an instance of a class with the methods you want to expose.
class Method
{
foo()
{
return 'hello';
}
bar(a, b)
{
return a + b
}
baz({name})
{
return 'hello ' + name;
}
}
const methods = new Method();
You can optionally include a schema
object within methods
for defining JSON schemas for methods
which expect named (i.e. destructured) parameters.
methods.schema =
{
baz:
{
properties:
{
name:
{
type: 'string'
}
},
minProperties: 1,
additionalProperties: false
}
}
Define your config:
const config =
{
instance: methods,
logger: true,
wsOptions:
{
port: 33333
}
}
instance
{Object} Object containing the methods exposed to the RPC server.logger
{Boolean|Function} Set to true to have debug log written to the console or pass a function to receive the log messages. Default = false
.wsOptions
{Object} Options passed to ws.Instantiate a new RPC server
const rpc = new Respect(config);
<methodName>._requestHeaders
or this._requestHeaders
depending on whether your method has a lexical this
.