0.2.0 • Published 6 years ago

smooth-rpc v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Smooth RPC

Smooth RPC is a library for making remote procedure calls in an intuitive way. Just declare functions on the server element and call them from the client element, that's it. If the function errors, the error will seamlessly be transferred to the calling client. It has no dependencies and works by utilizing Proxies that was introduced with ES6.

Install

npm install smooth-rpc

Examples

Server

Initiate a server, then just define your function on the server object.

const srpc = require('smooth-rpc');
const server = srpc.createServer();

server.divide = (arg1, arg2) => {
    if (arg2 === 0) {
        throw new Error("Can't divide by zero.");
    }
    return arg1 / arg2;
};

Client

Define where the server is located when creating a client. Then you can just call the function that is defined at the server and you get a promise that returns what the server function will return.

const srcp = require('smooth-rpc');
const remote = srcp.createClient({ host: '127.0.0.1' });

remote
    .divide(12, 3)
    .then(console.log)
    .catch(console.log);

If you are on >=Node 8.0.0, you can use the call with await if you are within a async function.

try {
    let result = await remote.divide(12, 0);
    console.log(result);
} catch (error) {
    console.log(error);
}

Options

Server

OptionDefaultDescription
host"0.0.0.0"The host that the server listen on
port6356The port that the server listen on
includeStacktrueShould errors include the server stacktrace?

Client

OptionDefaultDescription
host"127.0.0.1"The host that the server listen on
port6356The port that the server listen on
0.2.0

6 years ago

0.1.0

6 years ago