zmq-json-rpc-client
Version:
JSON-RPC 2.0 client implementation using a ZMQ socket as transport mechanism
87 lines (50 loc) • 2.05 kB
Markdown
JSON-RPC 2.0 client implementation using a ZeroMQ socket as transport mechanism.
More precisely a `dealer` socket is used for the client, in line with the [asynchronous clients / servers pattern](http://zguide.zeromq.org/page:all#The-Asynchronous-Client-Server-Pattern)
of ZeroMQ.
For corresponding server implementation, see [zmq-json-rpc-server](https://github.com/claudijo/zmq-json-rpc-server).
```
npm install zmq-json-rpc-client
```
Create a JSON RPC client with a ZeroMQ endpoint and emit requests or
notifications.
This module exports a factory faction that accepts a ZeroMQ endpoint, eg.
`'tcp:127.0.0.1:3030'` and options.
Options can be passed to the factory function as an object, specified by the
following keys and values.
Value: Function that is called each time a new request is created. Defaults to a
built-in incrementer starting at `1`.
Value: The number of milliseconds the client will wait for a reply when sending
a request, until returning a timeout error. Defaults to `30000` (30 seconds).
Value: Identity that will be used for the underlaying ZeroMQ socket. Defaults to
five random bytes.
Sends a JSON-RPC request or notification. Params and callback are optional. A
notification is sent if callback is omitted. A request that expetct a reply
will be sent if passing a callback. The callback will receive `error` and
`result` as arguments.
Exposes the underlying ZeroMQ socket object.
```js
var zmqJsonRpcClient = require('zmq-json-rpc-client');
var client = zmqJsonRpcClient('tcp:127.0.0.1', { socketId: 'client' });
// Send a notification
client.emit('update', [1, 2, 3, 4, 5]);
// Send a request
client.emit('subtract', {"subtrahend": 23, "minuend": 42}, function(err, result) {
// ...
});
```
Run unit tests:
`$ npm test`
MIT