

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
Tools
Check jsonrpc2-tools for some nice additions to this module.
Install
To install node-jsonrpc2 in the current directory, run:
npm install json-rpc2 --save
Changes from 1.x
- Uses native EventEmitter instead of EventEmitter3
Changes from 0.x
- Before, the
id member was permissive and wouldn't actually adhere to the RFC, allowing anything besides undefined.
- If your application relied on weird id constructs other than
String, Number or null, it might break if you update to 1.x
Usage
Firing up an efficient JSON-RPC server becomes extremely simple:
var rpc = require('json-rpc2');
var server = rpc.Server.$create({
'websocket': true,
'headers': {
'Access-Control-Allow-Origin': '*'
}
});
function add(args, opt, callback) {
callback(null, args[0] + args[1]);
}
server.expose('add', add);
server.expose('namespace', {
'function1': function(){},
'function2': function(){},
'function3': function(){}
});
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');
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();
server.enableAuth('user', 'pass');
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();
client.newFunction();
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();
rpc.Client.newFunction();
rpc.Server.newFunction();
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($super, direction, message){
$super(' (' + direction + ')', message);
}
});
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.myOwnClassMethod();
MyCoolServer.$create().myOwnFunction();
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.