1.1.0 • Published 4 years ago

json-rpc-constructor v1.1.0

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

JSON-RPC Constructor

A building block to create powerfull JSON-RPC apis

NPM Version Downloads Stats

Most JSON-RPC implementations out there strongly separate the server and client in the implementation and is directly coupled to an underlaying transport. This library gives you the building blocks to create any JSON-RPC api over any transport. It could be bidirectional over eg. websocket or a regular HTTP server-client setup.

Installation

yarn add json-rpc-constructor

or

npm install json-rpc-constructor --save

Usage example

import JSONRPC from 'json-rpc-constructor'
import WebSocket = require('ws')

const ws = new WebSocket('ws://www.example.com/json-rpc')

const rpc = new JSONRPC({ send(data) {
  ws.send(data)
}})
ws.on('message', (data: string) => {
  rpc.receive(data)
})

rpc.method('subtract', async (params) => {
  return params[0] - params[1]
})

rpc.notification('foobar', (params) => {
  console.log('got incoming foobar notification', params)
})

ws.on('open', () => {
  rpc.call('get-server-time', (result) => {
    console.log('server time is', result)
  })
})

Meta

Christian Vaagland Tellnes – github.com/tellnes

Distributed under the MIT license. See LICENSE for more information.

Contributing

  1. Fork it (https://github.com/tellnes/json-rpc-constructor/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request