Jayson is a simple but featureful JSON-RPC 2.0/1.0 client and server for node.js
Jayson is a simple but featureful JSON-RPC 2.0/1.0 client and server for node.js
Jayson is a JSON-RPC 2.0 and 1.0 compliant server and client written in JavaScript for node.js that aims to be as simple as possible to use.
A basic JSON-RPC 2.0 server via HTTP:
Server example in examples/simple_example/server.js:
const jayson = require('jayson');
// create a server
const server = new jayson.Server({
add: function(args, callback) {
callback(null, args[0] + args[1]);
}
});
server.http().listen(3000);Client example in examples/simple_example/client.js invoking add on the above server:
const jayson = require('jayson');
// create a client
const client = jayson.Client.http({
port: 3000
});
// invoke "add"
client.request('add', [1, 1], function(err, response) {
if(err) throw err;
console.log(response.result); // 2
});Install the latest version of jayson from npm by executing npm install jayson in your shell. Do a global install with npm install --global jayson if you want the jayson client CLI in your PATH.
maxBatchLengthlodash dependency which should halve bundle size. There might be minor incompatibilities if you pass funky object or array types to jayson methods.collect option removed from jayson.Server/Method. JSON-RPC params to handlers are now always in the first argument.collect: true is now the default option for a new jayson.Server and jayson.MethodThere is a basic CLI client in bin/jayson.js and it should be available as jayson in your shell if you installed the package globally. Run jayson --help to see how it works.
Jayson does not have any special dependencies that cannot be resolved with a simple npm install or yarn install.
In addition to this document, a comprehensive class documentation made with jsdoc is available at jayson.tedeh.net.
npm install --devnpm run testnpm run test-tscnpm run coverageSince v2.1.0 there is typescript support available with jayson.
If you encounter any problems with the type definitions, see the Contributing section.
The client is available as the Client or client property of require('jayson').
| Name | Description |
|---|---|
Client |
Base class |
Client.tcp |
TCP sub-class |
Client.tls |
TLS sub-class |
Client.http |
HTTP sub-class |
Client.https |
HTTPS sub-class |
Client.browser |
Standalone class |
Client.websocket |
Websocket sub-class |
Every client supports these options:
| Option | Default | Type | Description |
|---|---|---|---|
reviver |
undefined |
Function |
JSON.parse reviver |
replacer |
undefined |
Function |
JSON.stringify replacer |
generator |
RFC4122 generator | Function |
Generates a String for request ID. |
version |
2 | Number |
JSON-RPC version to support (1 or 2) |
notificationIdNull |
false |
Boolean |
Since 3.6.1. When true "id" property of a request will be set to null when version 2. |
Uses the same options as http.request in addition to these options:
| Option | Default | Type | Description |
|------------ |------------ |---------- |---------------------------------------- |
| encoding | utf8 | String | Determines the encoding to use |
| headers | undefined | Object | Extend the headers sent by the client |
The HTTP client will emit the following events:
| Event | When | Arguments | Notes |
|----------------- |----------------------------------- |--------------------------------------------------------------------------- |------------------------------------------- |
| http request | Created an HTTP request | 1. Instance of http.ClientRequest | |
| http response | Received an HTTP response | 1. Instance of http.IncomingMessage 2. Instance of http.ClientRequest | |
| http error | Underlying stream emits error | 1. Error | |
| http timeout | Underlying stream emits timeout | | Automatically causes the request to abort |
It is possible to pass a string URL as the first argument. The URL will be run through url.parse. Example:
const jayson = require('jayson');
const client = jayson.Client.http('http://localhost:3000');
// client.options is now the result of url.parseUses the same options as https.request in addition to the same options as Client.http. This means it is also possible
to pass a string URL as the first argument and have it interpreted by url.parse.
Will emit the same custom events as Client.http.
Uses the same options as net.connect.
Since version 3.5.1
The TCP client will emit the following events:
| Event | When | Arguments | Notes |
|---|---|---|---|
| tcp socket | TCP socket is opened | 1. net.Socket |
Can be used to setup timeouts |
| tcp error | TCP socket emits error | 1. Error emit by net.Socket |
Uses the same options as tls.connect.
Since version 3.5.1
The TLS client will emit the following events:
| Event | When | Arguments | Notes |
|---|---|---|---|
| tcp socket | TCP socket is opened | 1. net.Socket |
Can be used to setup timeouts |
| tcp error | TCP socket emits error | 1. Error emit by net.Socket |
The browser client is a simplified version of the regular client for use browser-side. It does not have any dependencies on node.js core libraries, but does depend on the uuid package for generating request ids. It also does not know how to "send" a request to a server like th
No open issues yet, or sync has not completed.