GithubHelp home page GithubHelp logo

fib-rpc's Introduction

fib-rpc

NPM version Build Status Build status

Remote Procedure Calling for fibjs

Introduction

fib-rpc is working on dispatching of remote procedure calling, it supports both http request and websocket(recommended).

obviously, it follows JSON-RPC protocol.

Get Started

npm i -S fib-rpc

Usage

you can use it with http.Request

var js_remote = rpc.handler({
    foo: function (p1, p2) {
        return p1 + ',' + p2;
    }
});

function js_call(r) {
    var m = new http.Request();

    m.value = 'test/tttt/tttt/';
    m.json(r);

    js_remote(m);

    m.response.body.rewind();
    return m.response.readAll().toString();
}

var result = js_call({
    // method is required
    method: 'foo',
    // params is required and must be array
    params: [100, 200],
    // id is required
    id: 1234
})

assert.equal(result, '{"id":1234,"result":"100,200"}');

but in most cases, you may prefer using it based on Websocket, which finshied by rpc.connect

const ws = require('ws')
const http = require('http')

const rpc = require('fib-rpc')

const svr = new http.Server(8811, ws.upgrade(
    rpc.handler(
        {
            test: function (v1, v2) {
                return v1 + v2;
            }
        }
    ))
);
svr.asyncRun();

const remoting = rpc.connect("ws://127.0.0.1:8811");

remoting.test(1, 2) // 3

Learn connect from test case 'websocket rpc' in exmaple.

Custom Errors

use RpcError as typed Error thrown if required:

const rpc = require('fib-rpc')

const js_remote = rpc.handler(
    {
        integerAdd: function (v1, v2) {
            if (!Number.isInteger(v1) || !Number.isInteger(v2))
                throw rpc.rpcError(4010000, 'Addend must be integer')
                
            return v1 + v2;
        }
    }
);

const svr = new http.Server(8811, ws.upgrade(js_remote));

const remoting = rpc.connect("ws://127.0.0.1:8811");

try {
    remoting.integerAdd(1.1, 2)
} catch (err_msg) {
    console.log(err_msg) // 'Addend must be integer'
}

console.log(
    js_call({
        method: 'foo',
        params: [1.1, 2],
        id: 1234
    })
) // code: 4010000, message: 'Addend must be integer'

Samples

View Samples, test them like this:

# build lib first.
npm i && npm run build

# chdir to examples
cd exapmles
npm i

# run example
fibjs ./connect.js
fibjs ./open_handler-js.js

fib-rpc's People

Contributors

richardo2016 avatar xicilion avatar

Watchers

 avatar  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.