zmq-json-rpc-server

JSON-RPC 2.0 server implementation using a ZMQ socket as transport mechanism

Usage no npm install needed!

<script type="module">
  import zmqJsonRpcServer from 'https://cdn.skypack.dev/zmq-json-rpc-server';
</script>

README

ZMQ JSON-RPC Server

JSON-RPC 2.0 server implementation using a ZeroMQ socket as transport mechanism. More precisely a router socket is used for the server, in line with the asynchronous clients / servers pattern of ZeroMQ.

For corresponding client implementation, see zmq-json-rpc-client.

Installation

npm install zmq-json-rpc-server

Usage

Create a JSON RPC server with a ZeroMQ endpoint and receive requests or notifications.

server = zmqJsonRpcServer(endpoint, options)

This module exports a factory faction that accepts a ZeroMQ endpoint, eg. 'tcp:127.0.0.1:3030' and options.

Options

Options can be passed to the factory function as an object, specified by the following key and value.

ignoreVersion

Value: Boolean indicating if server should ignore the jsonrpc member in the request. Defaults to false.

server.on(method, callback)

Listens for a JSON-RPC request or notification. The callback will receive params and reply arguments. The reply argument is a noop if the client sends a notification (ie. request without an id member). The reply argument should be called with arguments error and result when replying to a request.

server.socket

Exposes the underlying ZeroMQ socket object.

Example


var zmqJsonRpcServer = require('zmq-json-rpc-server');
var server = zmqJsonRpcServer('tcp:127.0.0.1');

// Listen for a notification
server.on('update', function(params) {
  // ..  
});

// Listen for a request and send reply
server.on('subtract', function(params, reply) {
  reply(null, params.minuend - params.subtrahend);
});

// Exiting application
process.on('SIGINT', function() {
  server.socket.close();
});

Test

Run unit tests:

$ npm test

License

MIT