Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pkoretic/json_rpc
Fast and lightweight persistent promise based JSON RPC 2.0 client implementation over TCP and Unix socket
https://github.com/pkoretic/json_rpc
Last synced: 6 days ago
JSON representation
Fast and lightweight persistent promise based JSON RPC 2.0 client implementation over TCP and Unix socket
- Host: GitHub
- URL: https://github.com/pkoretic/json_rpc
- Owner: pkoretic
- License: mit
- Created: 2015-03-14T13:41:26.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-27T20:00:34.000Z (about 9 years ago)
- Last Synced: 2024-08-09T12:10:16.492Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 197 KB
- Stars: 9
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
json-rpc client
===============[![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/pkoretic/json-rpc-client/blob/master/LICENSE)
[![NPM](https://nodei.co/npm/json-rpc-client.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/json-rpc-client/)JSON-RPC 2.0 TCP implementation with persistent connections using promises - very fast and without dependencies
## Installation
npm install json-rpc-client
## Example usage
### Native promisevar jsonrpc = require('json-rpc-client')
// create client and connect
var client = new jsonrpc({ port: 7070, host: '127.0.0.1'})
client.connect().then(function()
{
// send json rpc
client.send('add', [1,2]).then(function(reply)
{
// print complete reply
console.log(reply)
},
//transport errors
function(error)
{
console.error(error)
})
},
function(error)
{
console.error(error)
})### Generators (using co)
var jsonrpc = require('json-rpc-client')
var co = require('co')// create client and connect
var client = new jsonrpc({ port: 7070, host: '127.0.0.1'})co(function*()
{
try
{
yield client.connect()// send json rpc
var reply = yield client.send('add', [1,2])// print complete reply
console.log(reply)
}
catch(error)
{
console.error(error)
}
})## API
var jsonrpc = require('./jsonrpc')
### jsonrpc (options)
Creates a new RPC connection object.
Options:
* host: Host the client should connect to. Defaults to '127.0.0.1'.
* port: Port the client should connect to. Defaults to '7070'.
* keepalive [optional]: if set, keepalive will set using [net-keepalive](https://github.com/hertzg/node-net-keepalive)### connect
Returns promise which resolves after the connection to the specified host is ready### send (methodName, parameters, notification)
Sends json data through persisted tcp connection.
methodName: string
parameters: Object/Array with parameters
notification: true/false to make notification request (no reply)
* Promise - object containing reply data along with error
### close
Closes RPC connection and returns promise afterwards.
### Event 'error'
* 'Error Object'Emitted when an error occurs.
### Event: 'close'
* 'had_error' 'Boolean' true if the socket had a transmission errorEmitted once the RPC connection socket is fully closed. The argument
'had_error' is a boolean which says if there was an error.