GithubHelp home page GithubHelp logo

slashmanx / node-jsonrpc2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pocesar/node-jsonrpc2

0.0 1.0 0.0 490 KB

JSON-RPC 2.0 server and client library, with HTTP (with Websocket support) and TCP endpoints

License: Other

node-jsonrpc2's Introduction

Build Status

NPM

node-jsonrpc2

JSON-RPC 2.0 server and client library, with HTTP (with Websocket support) and TCP endpoints

This fork is a rewrite with proper testing framework, linted code, compatible with node 0.8.x and 0.10.x, class inheritance, and added functionalities

Install

To install node-jsonrpc2 in the current directory, run:

npm install json-rpc2 --save

Usage

Firing up an efficient JSON-RPC server becomes extremely simple:

var rpc = require('json-rpc2');

var server = rpc.Server.create({
    'websocket': true // is true by default
    'headers': { // allow custom headers is empty by default
        'Access-Control-Allow-Origin': '*'
    }
});

function add(args, opt, callback) {
  callback(null, args[0] + args[1]);
}

server.expose('add', add);

// you can expose an entire object as well:

server.expose('namespace', {
    'function1': function(){},
    'function2': function(){},
    'function3': function(){}
});
// expects calls to be namespace.function1, namespace.function2 and namespace.function3

// listen creates an HTTP server on localhost only
server.listen(8000, 'localhost');

And creating a client to speak to that server is easy too:

var rpc = require('json-rpc2');

var client = rpc.Client.create(8000, 'localhost');

// Call add function on the server

client.call('add', [1, 2], function(err, result) {
    console.log('1 + 2 = ' + result);
});

Create a raw (socket) server using:

var rpc = require('json-rpc2');

var server = rpc.Server.create();

// non-standard auth for RPC, when using this module using both client and server, works out-of-the-box
server.enableAuth('user', 'pass');

// Listen on socket
server.listenRaw(8080, 'localhost');

Extend, overwrite, overload

Any class can be extended, or used as a mixin for new classes, since it uses ES5Class module.

For example, you may extend the Endpoint class, that automatically extends Client and Server classes. Extending Connection automatically extends SocketConnection and HttpServerConnection.

var rpc = require('json-rpc2');

rpc.Endpoint.include({
    'newFunction': function(){

    }
});

var
    server = rpc.Server.create(),
    client = rpc.Client.create();

server.newFunction(); // already available
client.newFunction(); // already available

To implement a new class method (that can be called without an instance, like rpc.Endpoint.newFunction):

var rpc = require('json-rpc2');

rpc.Endpoint.implement({
    'newFunction': function(){
    }
});

rpc.Endpoint.newFunction(); // available
rpc.Client.newFunction(); // every
rpc.Server.newFunction(); // where

Don't forget, when you are overloading an existing function, you can call the original function using $super

var rpc = require('json-rpc2');

rpc.Endpoint.implement({
    'trace': function(direction, message){
        this.$super(' (' + direction + ')', message); //call the last defined function
    }
});

And you can start your classes directly from any of the classes

var MyCoolServer = require('json-rpc2').Server.define('MyCoolServer', {
    myOwnFunction: function(){
    },
}, {
    myOwnClassMethod: function(){
    }
}); // MyCoolServer will contain all class and instance functions from Server

MyCoolServer.myOwnClassMethod(); // class function
MyCoolServer.create().myOwnFunction(); // instance function

Debugging

This module uses the debug package, to debug it, you need to set the Node environment variable to jsonrpc, by setting it in command line as set DEBUG=jsonrpc or export DEBUG=jsonrpc

Examples

To learn more, see the examples directory, peruse test/jsonrpc-test.js, or simply "Use The Source, Luke".

More documentation and development is on its way.

node-jsonrpc2's People

Contributors

bbigras avatar blakmatrix avatar dcharbonnier avatar egolovaniuc avatar ericflo avatar falsecz avatar justmoon avatar masim05 avatar pocesar avatar topaxi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.