Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including simple-jsonrpc-js with all npm packages installed. Try it out:

var simpleJsonrpcJs = require("simple-jsonrpc-js")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

simple-jsonrpc-js v1.0.1

simple jsonrpc client/server handler


  • Client and server.
  • Support websocket and http.
  • Full support json-rpc 2.0


You can install this package either with

bower install simple-jsonrpc-js # on webclient
npm install simple-jsonrpc-js # on nodejs


(over WebSocket) on server:

function add(x, y){
    return x + y;

//over ws
var WebSocketServer = new require('ws');
var JsonRPC = require('simple-jsonrpc-js');

var webSocketServer = new WebSocketServer.Server({
    host: '',
    port: 8090
}).on('connection', function(ws) {
    var jrpc = new JsonRPC();
    ws.jrpc = jrpc;

    ws.jrpc.toStream = function(message){

    ws.on('message', function(message) {

    jrpc.on('add', ['x', 'y'], add);

    jrpc.on('mul', ['x', 'y'], function(x, y){
        return x*y;

    var item_id = 120;

    jrpc.on('create', ['item', 'rewrite'], function(item, rewrite){
        item_id ++; = item_id;
        return item;
    });'view.setTitle', ["JSONRPC over ws"]);


And on client:

        var jrpc = new simple_jsonrpc();
        var socket = new WebSocket("ws://localhost:8090");

        //wait of call
        jrpc.on('view.setTitle', function(title){
            document.getElementsByClassName('title')[0].innerHTML = title;

        socket.onmessage = function(event) {

        jrpc.toStream = function(_msg){

        socket.onerror = function(error) {
            console.error("Error: " + error.message);

        socket.onclose = function(event) {
            if (event.wasClean) {
      'Connection close was clean');
            } else {
                console.error('Connection suddenly close');
  'close code : ' + event.code + ' reason: ' + event.reason);

        //after connect
        socket.onopen = function(){

  'add', [2, 3]).then(function (result) {
                document.getElementsByClassName('paragraph')[0].innerHTML += 'add(2, 3) result: ' + result + '<br>';

  'mul', {y: 3, x: 2}).then(function (result) {
                document.getElementsByClassName('paragraph')[0].innerHTML += 'mul(2, 3) result: ' + result + '<br>';

                {call:{method: "add", params: [5,2]}},
                {call:{method: "mul", params: [100, 200]}},
                {call:{method: "create", params: {item: {foo: "bar"}, rewrite: true}}}

More examples in directory


call(methodName, params) - Remote method invocation. Returned promise object.

notification(methodName, params) - Remote method invocation without response object

batch(calls) - batch invocation. Returned promise object.

on(methodName, paramsName, handler) - Registration local method for incommig invocation

off(methodName) - disable method for incommig invocation

customException(code, message, data) - return exception with implementation-defined server-errors


messageHandler(rawMessage) - All incoming messages must be passed as a parameter. Returned promise object.

toStream - The property, a function pointer. It is necessary to determine before use. Will be called for send a message to the remote host


  • nodejs <= 4 || latest browser
  • lodash
  • promise-polyfill for old versions nodejs and browsers


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free