Skip to content
JSON-RPC 2.0 Server implementation that supports multiple transport protocols.
TypeScript JavaScript
Branch: master
Clone or download
Latest commit 16f2ddc Aug 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
src fix: test coverage Aug 20, 2019
test-cert feat: second Apr 6, 2019
.editorconfig feat: second Apr 6, 2019
.gitignore feat: cleanupcrew Apr 24, 2019
.releaserc feat: second Apr 6, 2019
README.md fix: add badges May 15, 2019
jest.config.js feat: second Apr 6, 2019
package-lock.json
package.json build(deps): bump commander from 2.20.0 to 3.0.0 Aug 9, 2019
tsconfig.json feat: cleanupcrew Apr 24, 2019
tsfmt.json feat: second Apr 6, 2019
tslint.json feat: second Apr 6, 2019

README.md

OpenRPC Server JS

CircleCI branch Dependabot status Chat on Discord npm GitHub release GitHub commits since latest release

JSON-RPC 2.0 Server implementation that supports multiple transport protocols. Built to run with node 10+.

Inspired by mole-rpc, built for OpenRPC.

Features

How to Use

CLI

Install

npm install -g @open-rpc/server-js

Start the server

open-rpc-server-js \
  -c ./open-rpc-server.config.json \
  -h ./method-handlers \
  -s https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/simple-math-openrpc.json

Thats it!


Javascript/Typescript API

Install

npm install --save @open-rpc/server-js

Creating Routers

using method mapping and OpenRPC document
import { types } from "@open-rpc/meta-schema";
import { Router } from "@open-rpc/server-js";

const openrpcDocument = {
  openrpc: "1.0.0",
  info: {
    title: "node-json-rpc-server example",
    version: "1.0.0"
  },
  methods: [
    {
      name: "addition",
      params: [
        { name: "a", schema: { type: "integer" } },
        { name: "b", schema: { type: "integer" } }
      ],
      result: {
        { name: "c", schema: { type: "integer" } }
      }
    }
  ]
} as types.OpenRPC;

const methodHandlerMapping = {
  addition: (a: number, b: number) => Promise.resolve(a + b)
};

const router = new Router(openrpcDocument, methodHandlerMapping);
mock mode
const router = new Router(openrpcDocument, { mockMode: true });

Creating Transports

IPC
import { TCPIPCServerTranport, UDPIPCServerTranport } from "@open-rpc/server-js";

const ipcOptions = { maxConnetions: 20 }; // https://www.npmjs.com/package/node-ipc#ipc-config
const TCPIPCOptions = { ...ipcOptions, networkPort: 4343 };
const UDPIPCOptions = { ...ipcOptions, networkPort: 4343, udp: true };

const tcpIpcTransport = new IPCServerTranport(TCPIPCTransportOptions);
const UdpIpcTransport = new IPCServerTranport(UDPIPCTransportOptions);
HTTP/S
import { HTTPServerTransport, HTTPSServerTransport } from "@open-rpc/server-js";

const httpOptions = {
  middleware: [ cors({ origin: "*" }) ],
  port: 4345
};
const httpsOptions = { // extends https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener
  middleware: [ cors({ origin: "*" }) ],
  port: 4346,
  key: await fs.readFile("test/fixtures/keys/agent2-key.pem"),
  cert: await fs.readFile("test/fixtures/keys/agent2-cert.pem"),
  ca: fs.readFileSync("ssl/ca.crt")
};

const httpTransport = new HTTPServerTransport(httpOptions);
const httpsTransport = new HTTPSServerTransport(httpsOptions); // Defaults to using HTTP2, allows HTTP1.
WebSockets
import { WebSocketServerTransport } from "@open-rpc/server-js";

const webSocketFromHttpsOptions = { // extends https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
  server: httpsTransport.server
};

const webSocketOptions = { // extends https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
  port: 4347
};
const wsFromHttpsTransport = new WebSocketServerTransport(webSocketFromHttpsOptions); // Accepts http transport as well.
const wsTransport = new WebSocketServerTransport(webSocketOptions); // Accepts http transport as well.

Creating the server

With everything known upfront
import { Server } from "@open-rpc/server-js";
import { petstore } from "@open-rpc/examples";

const options = {
  router: router,
  transportConfigs: [
    { type: "IPCTransport", options: { port: "8001" } },
    { type: "IPCTransport", options: { port: "8001", udp: true } },
    { type: "HTTPTransport", options: { port: "8002" } },
    { type: "HTTPSTransport", options: { port: "8003", cert: "...", key: "..." } },
    { type: "WebSocketTransport", options: { port: "8005" } },
    { type: "WebSocketTransport", options: { port: "8004", cert: "...", key: "..." } },
  ],
  openrpcDocument: petstore
};

const server = new Server(options);

server.start();
Add components as you go
const server = new Server();
server.start();

server.addTransport(httpsTransport); // will be started immediately
server.setRouter(router);
server.addTransports([ wsTransport, wsFromHttpsTransport, httpsTransport ]); // will be started immediately.
You can’t perform that action at this time.