Code Monkey home page Code Monkey logo

rpcify's Introduction

RPCify node.js modules

This module allows to expose the functions of node.js modules via JSON-RPC, which makes them available to other programs written in any language.

Install

npm install

Usage

var rpcify = require('rpcify');

var headlessWallet = require('bng-headless');  // this is a module whose methods you want to expose via RPC
var balances = require('bmg-core/balances.js'); // another such module

// start listening on RPC port
rpcify.listen(6333, '127.0.0.1');

// expose some functions via RPC
rpcify.expose(headlessWallet.issueChangeAddressAndSendPayment);
rpcify.expose(balances.readBalance, true);
rpcify.expose([headlessWallet.readFirstAddress, headlessWallet.readSingleWallet, headlessWallet.issueOrSelectAddressByIndex], true);

After this point, the above functions will become available via JSON-RPC interface. Node.js clients need to call

var rpc = require('json-rpc2');

var client = rpc.Client.$create(6333, '127.0.0.1');

client.call('issueChangeAddressAndSendPayment', [asset, amount, to_address, device_address], function(err, unit) {
    ...
});

which is similar to calling a function from a require()ed module. Programs in other languages can also access the exposed node.js functions via RPC.

API

You can expose any function that returns its result via callback. The callback must be the last argument of the function.

The parameters of the JSON-RPC request will be passed to the function as arguments. The parameters can be of any type that JSON supports: string, number, boolean, object, but not function. If the params of the JSON-RPC request is an array, it will be expanded to the list of arguments. If it is an object, it will be passed as the 1st argument to the function.

The parameters of the callback will be returned in the JSON-RPC response. By default, it is assumed that the first argument of the callback is an error. If it is truthy, the request will end up with an error response:

{"jsonrpc":"2.0","error":{"code":-32603,"message":"not enough spendable funds from 4MIACTR3WJMC4UUKHHU3VX55LWW77EZW,QM63QOKZVOHGTMCKRQCB4GJVD4FCHL56 for 1000000000641"}

If the first argument of the callback is falsy, the request succeeds. If there is only one remaining argument, it will be returned as the result of the JSON-RPC request. If there are more remaining arguments, they will be wrapped in an array.

If the optional bNoErrors parameter of expose() (see below) is truthy, the request never ends with an error and the first and subsequent parameters of the callback are interpreted as the result.

To expose a function under its original name, use

rpcify.expose(func, bNoErrors);

To expose several functions under their original names, pass an array of functions:

rpcify.expose([func1, func2], bNoErrors);

To expose functions under different names, use this syntax:

rpcify.expose('func_name', func, bNoErrors);
rpcify.expose({func1_name: func1, func2_name: func2}, bNoErrors);

rpcify's People

Contributors

droidzf avatar tonyofbyteball avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

wintchain

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.