Viktor Turskyi
Yet another JSON RPC Library?
KyivJS
Viktor Turskyi
● CEO and architect at WebbyLab
● 15 years in software development
● Delivered more than 60 projects of
different scale
JSON RPC? Another library? Really?
Average JS developer thinks that this is JSON RPC
Exercise 1: connect two services via websocket
Simple solution
→ {"call": "subtract", "data": [42, 23], "id": 1}
← {"result": 19, "id": 1}
The same but with JSON RPC
Not JSON RPC:
→ {"call": "subtract", "data": [42, 23], "id": 1}
← {"result": 19, "id": 1}
JSON RPC:
→ {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
← {"jsonrpc": "2.0", "result": 19, "id": 1}
Ok.. JSON-RPC.. But why own library?
Initial architecture
Core services introduced
Internet clients introduced
Why JSON RPC?
● The simplest RPC (request, response) standard.
● Transport agnostic (supports “IP over Avian Carriers”, rfc1149)
● Uses good old JSON.
● Supports batch calls.
● Tiny specification
● Battle tested.
● While implementing communication protocol you can accidentally implement
own JSON RPC :)
What I want?
● Transport agnostic (WS, HTTP, HTTPS, MQTT, Avian Carriers, WebRTC etc)
● Multi transport mode with transport dynamic registration
● Bidirectional communication over the same channel
● Modern API
● Server and client both working in browser
● Lightweight, ideally core library with zero dependencies
● Easy to implement own transports (all hard things should be abstracted)
● Easy way to test new transports
Let’s search NPM
Why not jayson?
Why not mqtt-json-rpc?
Why not jsonrpc-bidirectional?
Let’s take a look at source code
https://github.com/bigstepinc/jsonrpc-bidirectional/tree/master/src
Why not modular-json-rpc?
https://www.npmjs.com/package/jayson - great library but rather coupled, hard to
extend, server requires all transports. It good for own use cases but not universal.
https://www.npmjs.com/package/jsonrpc-bidirectional - I’ve never thought that
JSON RPC can be so complex. Later about this.
https://github.com/teambition/jsonrpc-lite - just request/response serializer/parser
https://www.npmjs.com/package/modular-json-rpc - one transport example, does
not support multiple transports, batches not implemented, no tests, 2 stars on GH
https://www.npmjs.com/package/mqtt-json-rpc - leaky API, transport and payload
are interconnected
I tried a lot and was not able to find the right one
So, I’ve created a new JSON RPC Library
Nope, this is not it
This is it!
Mole RPC API
Mole Server API
Mole Client API
Mole Transports API
Mole Server API
Server API: (use strategy pattern)
Server API: direct expose (no wrappers, not rpc details etc)
Mole Client API
Client API v1: Can we make better?
Client API v2: Can we make better?
Client API v3: Use object as is
Mole RPC Transports API
Transport API v1: Can we make better?
Transport API v2: Can we make better?
Transport API v3: Current
AutoTester for your transport
Unusual use cases
Use case 1: JSON RPC Over MQTT
Why to use: all your service needs is just a MQTT endpoint. After that it can
communicate with any service and any other service can communicate with it.
Your service can be same time client and server.
We use Mosquitto MQTT server with Mole RPC transport for MQTT
Why to use: your server is behind NAT/Firewall and you want to send request to
it. So, you server can connect to client via websocket and now client can send
requests to connected server as usual.
Here are working examples:
● Server connects to client
● Bidirectional calls
Use case 2: inversion of client and server
Use case 3: send tasks to browser Web Worker
Why to use: everytime using web
workers you will need match
request and response. You can do
it with own protocol or use feature
rich, spec compliant Mole-RPC
with Web Worker transport
3.5kb (min+gzip) client build
< 2kb (min+gzip) server build
Use case 3: factory for Web Worker transport
Use case 4: JSON RPC Over event emitter???
JS features that allow to build better abstractions in MoleRPC
Classes
Promises + Async/Await
Proxies
Symbols (for providing access to metadata during request)
What is next?
Improve documentation, add more examples.
Release v1.0 with finalized API. MoleRPC itself is almost finalized but some
features can be added to transports.
Improve existing transports.
Create more transports: HTTP(S), TCP, WebRTC
More production testing etc
Links
https://www.npmjs.com/package/mole-rpc
https://www.npmjs.com/package/mole-rpc-transport-mqtt
https://www.npmjs.com/package/mole-rpc-transport-webworker
https://www.npmjs.com/package/mole-rpc-transport-eventemitter
https://www.npmjs.com/package/mole-rpc-transport-ws
Telegram: @JABASCRIPT
Viktor Turskyi
viktor@webbylab.com
@koorchik @koorchik
https://webbylab.com

Yet another json rpc library (mole rpc)