New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

jwrpc

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jwrpc

bidirectional rpc using Websocket and JSON

latest
Source
npmnpm
Version
0.0.6
Version published
Maintainers
1
Created
Source

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

  • Usage examples

  • FAQ

  • Need Help? Contant Me

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

Keywords

rpc

FAQs

Package last updated on 21 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts