Skip to content

mhingston/respect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ResPeCt

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.

Installation

npm install mhingston/respect

Usage

// 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);

Notes

  • Supports JSON-RPC 2.0 only.
  • Calls to methods without named parameters must have the same number of arguments as the method signature.
  • Supports async methods, i.e. returning a promise. Callbacks are not supported as functions don't "JSONify".
  • You can access the client headers sent with the upgrade request from within a method by accessing the property <methodName>._requestHeaders or this._requestHeaders depending on whether your method has a lexical this.
  • Check out the tests for more examples.