A building block to create powerfull JSON-RPC apis
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.
yarn add json-rpc-constructor
or
npm install json-rpc-constructor --save
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)
})
})
Christian Vaagland Tellnes – github.com/tellnes
Distributed under the MIT license. See LICENSE
for more information.
- Fork it (https://github.com/tellnes/json-rpc-constructor/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request