Exposing RPC interface

If you are developing a service on Obyte and your programming language is node.js, your best option is to just require() the ocore modules that you need.

For exchanges, there is a specialized JSON RPC service. However it is limited and exposes only those functions that the exchanges need.

If you are developing a service on Obyte and your programming language is node.js, your best option is to just require() the ocore modules that you need (most likely you need headless-obyte and various modules inside ocore). This way, you'll also be running a Obyte node in-process.

If you are programming in another language, or you'd like to run your Obyte node in a separate process, you can still access many of the functions of headless-obyte and ocoreby creating a thin RPC wrapper around the required functions and then calling them via JSON-RPC.

Exposing functions and events

To expose the required functions via JSON-RPC, create a small node.js module that depends on headless-obyte, ocore (and any other ocore modules) and RPCify:

var rpcify = require('rpcify');
var eventBus = require('ocore/event_bus.js');
// this is a module whose methods you want to expose via RPC
var headlessWallet = require('headless-obyte');
var balances = require('ocore/balances.js'); // another such module
// most of these functions become available only after the passphrase is entered
eventBus.once('headless_wallet_ready', function(){
// start listening on RPC port
rpcify.listen(6333, '');
// expose some functions via RPC
rpcify.expose(balances.readBalance, true);
], true);
// expose some events
rpcify.exposeEvent(eventBus, "my_transactions_became_stable");
rpcify.exposeEvent(eventBus, "new_my_transactions");

Calling with HTTP requests

From another Node.js app, calling the function would look something like this:

var rpc = require('json-rpc2');
var client = rpc.Client.$create(6333, '');
client.call('issueChangeAddressAndSendPayment', [asset, amount, to_address, device_address], function(err, unit) {

Listening via WebSockets

From another Node.js app, sending function calls and listening to events would look something like this:

var WebSocket = require('ws');
var ws = new WebSocket("ws://");
ws.on('open', function onWsOpen() {
console.log("ws open");
})); // send a command
ws.on('error', function onWsError(e){
console.log("websocket error"+e);
ws.on('message', function onWsMessage(message){ // JSON responses
// {"jsonrpc":"2.0","result":{"base":{"stable":0,"pending":0}},"error":null,"id":1} // response to readBalance
// {"event":"my_transactions_became_stable","data":[["1pLZa3aVicNLE6vcClG2IvBe+tO0V7kDsxuzQCGlGuQ="]]} // event my_transactions_became_stable

Get started

Start this module (which also includes a Obyte node) and you'll be able to call the exposed functions via RPC from a program in any language. See the documentation about RPCify for more details.