@nodeguy/json-rpc
v0.3.2
Published
a simple JSON-RPC client
Downloads
518
Readme
This is a JSON-RPC client library. It is compliant with the JSON-RPC 2.0 Specification.
Why?
I wrote yet another JSON-RPC library in JavaScript because the existing solutions I looked at tied themselves to a specific transport (e.g., HTTP) or were more complicated to use than just calling a function. RPC stands for Remote Procedure Call so it should be as simple to use as calling a procedure.
Usage
You set up the library by giving it a transport which is a function that takes an AsyncIterable and returns an AsyncIterable. This simple interface gives it the power to work with any underlying transport (e.g., HTTP, WebSocket, etc.). HTTP and WebSocket transports are provided.
Once initialized, it returns an object called methods
that is an object representing the server. Calling a method on the server is as simple as calling the method on the methods
object. All methods return promises.
Examples
HTTP
const assert = require('assert')
const jsonRpc = require('@nodeguy/json-rpc')
const url = require('url')
const transport = jsonRpc.transport(url.parse('http://www.example.com'))
const client = jsonRpc.client(transport)
const server = client.methods
server.subtract(42, 23).then((response) => {
assert.equal(response, 19)
}
WebSocket
const assert = require('assert')
const jsonRpc = require('@nodeguy/json-rpc')
const url = require('url')
const transport = jsonRpc.transport(url.parse('ws://www.example.com'))
const client = jsonRpc.client(transport)
const server = client.methods
server.subtract(42, 23).then((response) => {
assert.equal(response, 19)
}
Copyright
Copyright 2016 David Braun
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
these files except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
. Unless required by
applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.