Skip to content
/ JwRPC Public

lightweight bidirectional rpc using ws and json

Notifications You must be signed in to change notification settings

UPO33/JwRPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JwRPC

lightweight bidirectional JSON RPC over Websocket

WIP don't use it yet

Features

  • Easy To use
  • Lightweight, no dependency just a single file
  • Bidirectional, both server and clients can send Request and Notification to eachother
  • Async and promise based
  • Basic rate limiting control

Table of contents

Installing

npm install jwrpc

Usage examples

Simple Server

const WebSocket = require('ws');
const JwRPC = require('jwrpc');

const wss = new WebSocket.Server({ port: 3434 });

const serverMethods = {
    'greet'  : new JwRPC.MethodInfo(RPC_greet),
    'echo'   : new JwRPC.MethodInfo(RPC_echo),
    'divide' : new JwRPC.MethodInfo(RPC_divide),
    
};



function RPC_greet(peer, params, callback){
    console.log({params});
    //for notification calling 'callback' is optional
}
function RPC_echo(peer, params, callback){
    console.log({params});
    //send the 'params' back to client
    callback(null, params);
}
function RPC_divide(peer, params, callback){
    console.log({params});
    if(params[1] === 0)
        return callback({code:1, message:'divide by zero'});
    
    callback(null, params[0] / params[1]);
}


wss.on('connection', function (ws) {
    console.log('new connection accepted');
    ws.conn = new JwRPC(ws, serverMethods);

});

Simple Client

const WebSocket = require('ws');
const JwRPC = require('jwrpc');

const ws = new WebSocket('ws://localhost:3434');

ws.on('open', function open() {
    const conn = new JwRPC(ws, {});

    //send a notification to server
    conn.Notify('greet', 'hi');

    //send a request to server.
    conn.Request('divide', [8, 2]).then(function(divideResult){
        console.log({divideResult}); //prints 4
    });
    //send a request to server. 
    conn.Request('divide', [8, 0]).catch(function(divideError){
        console.log({divideError}); //prints error. divide by zero.
    });
    //
    conn.Request('echo', 'hello world').then(function(echoResult){
        console.log({echoResult}); //prints 'hello world'
    });

});

Need Help? Contant Me

abc0d3r@gmail.com

About

lightweight bidirectional rpc using ws and json

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published