Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Yet another json rpc library (mole rpc)

159 views

Published on

Yet another JSON RPC library for JavaScript? In my talk, I will explain why I've created Mole RPC, show architectural patterns which were useful. I will show you what modern features of JavaScript helped me with API design.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Yet another json rpc library (mole rpc)

  1. 1. Viktor Turskyi Yet another JSON RPC Library? KyivJS
  2. 2. Viktor Turskyi ● CEO and architect at WebbyLab ● 15 years in software development ● Delivered more than 60 projects of different scale
  3. 3. JSON RPC? Another library? Really?
  4. 4. Average JS developer thinks that this is JSON RPC
  5. 5. Exercise 1: connect two services via websocket
  6. 6. Simple solution → {"call": "subtract", "data": [42, 23], "id": 1} ← {"result": 19, "id": 1}
  7. 7. 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}
  8. 8. Ok.. JSON-RPC.. But why own library?
  9. 9. Initial architecture
  10. 10. Core services introduced
  11. 11. Internet clients introduced
  12. 12. 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 :)
  13. 13. 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
  14. 14. Let’s search NPM
  15. 15. Why not jayson?
  16. 16. Why not mqtt-json-rpc?
  17. 17. Why not jsonrpc-bidirectional? Let’s take a look at source code https://github.com/bigstepinc/jsonrpc-bidirectional/tree/master/src
  18. 18. Why not modular-json-rpc?
  19. 19. 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
  20. 20. So, I’ve created a new JSON RPC Library
  21. 21. Nope, this is not it
  22. 22. This is it!
  23. 23. Mole RPC API Mole Server API Mole Client API Mole Transports API
  24. 24. Mole Server API
  25. 25. Server API: (use strategy pattern)
  26. 26. Server API: direct expose (no wrappers, not rpc details etc)
  27. 27. Mole Client API
  28. 28. Client API v1: Can we make better?
  29. 29. Client API v2: Can we make better?
  30. 30. Client API v3: Use object as is
  31. 31. Mole RPC Transports API
  32. 32. Transport API v1: Can we make better?
  33. 33. Transport API v2: Can we make better?
  34. 34. Transport API v3: Current
  35. 35. AutoTester for your transport
  36. 36. Unusual use cases
  37. 37. 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
  38. 38. 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
  39. 39. 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
  40. 40. Use case 3: factory for Web Worker transport
  41. 41. Use case 4: JSON RPC Over event emitter???
  42. 42. JS features that allow to build better abstractions in MoleRPC Classes Promises + Async/Await Proxies Symbols (for providing access to metadata during request)
  43. 43. 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
  44. 44. 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
  45. 45. Telegram: @JABASCRIPT
  46. 46. Viktor Turskyi viktor@webbylab.com @koorchik @koorchik https://webbylab.com

×