{"id":39546806,"url":"https://github.com/mhingston/jayson","last_synced_at":"2026-01-18T06:46:54.031Z","repository":{"id":219735573,"uuid":"120818288","full_name":"mhingston/jayson","owner":"mhingston","description":"JSON-RPC inspired server and client with HTTP and WebSocket transports and JSON Schema IDL","archived":false,"fork":false,"pushed_at":"2018-12-31T10:59:51.000Z","size":253,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-08T13:49:10.544Z","etag":null,"topics":["http","json-rpc","json-schema","nodejs","websocket"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mhingston.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-02-08T21:08:49.000Z","updated_at":"2025-01-07T11:49:59.000Z","dependencies_parsed_at":"2024-01-29T14:07:28.093Z","dependency_job_id":null,"html_url":"https://github.com/mhingston/jayson","commit_stats":null,"previous_names":["mhingston/jayson"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/mhingston/jayson","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhingston%2Fjayson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhingston%2Fjayson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhingston%2Fjayson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhingston%2Fjayson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mhingston","download_url":"https://codeload.github.com/mhingston/jayson/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhingston%2Fjayson/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28434103,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T18:57:19.464Z","status":"ssl_error","status_checked_at":"2026-01-14T18:52:48.501Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","json-rpc","json-schema","nodejs","websocket"],"created_at":"2026-01-18T06:46:53.832Z","updated_at":"2026-01-18T06:46:54.020Z","avatar_url":"https://github.com/mhingston.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Jayson\n\n[JSON-RPC](http://www.jsonrpc.org/specification) inspired server and client with HTTP and WebSocket transports and JSON Schema IDL.\n\n## Installation\n\n```\nnpm install mhingston/jayson\n```\n\n## Introduction\n\n### Format \n\nJayson uses a custom version of JSON-RPC for messages. The specification is based on [JSON-RPC 2.0](http://www.jsonrpc.org/specification) with the following changes:\n\n* The `request` and `response` objects **must** contain the property `jayson` with the value set to the version of the API being used. This property **replaces** the `jsonrpc` property.\n\n  e.g.\n\n  ```json\n  {\n      \"jayson\": \"1.0\"\n  }\n  ```\n\n* A `response` object without an `error` property and without a `result` property indicates that the remote method returned `undefined`.\n\n* The following additional properties can be defined within a `request` object:\n\n    * `auth` {String} A [JWT](https://github.com/auth0/node-jsonwebtoken) providing the authentication context.\n\nThe reason for baking auth into JSON-RPC was that I felt it should be part of the protocol and not reliant on the different authentication mechanisms available within the transport layers (i.e. HTTP, WebSocket).\n\n### Method Properties\n\nRemote methods may have attached to them the following properties:\n\n* `schema` {Object} A JSON schema which is used as the IDL. The schema must verify against the [method schema](https://github.com/mhingston/jayson/blob/master/schemas/1.0/method.json). If a method doesn't provide a full schema then any calls to the method won't have their `auth` or `params` properties validated. Similarly if the `returns` schema isn't defined then the return value won't be validated.\n\n    #### Example\n\n    ```javascript\n    function hello(name)\n    {\n        return 'Hello ' + name;\n    }\n\n    hello.schema =\n    {\n        params:\n        {\n            type: 'string'\n        },\n        returns:\n        {\n            type: 'string'\n        }\n    }\n    ```\n\n    If you need to reference schema definitions you should pass in your `definitions` schema to the server config. [Example](https://github.com/mhingston/jayson/blob/master/examples/schema-definitions).\n\n* `timeout` {Number} How long to wait (in milliseconds) before timing out the request. If not provided then the `timeout` value will be used from the server config.\n\n## Server Usage\n\n**Note: The server is intended to be run behind a reverse proxy, unless you're using [electron](https://github.com/electron/electron) ofcourse.**\n\n```javascript\n// Import the module\nconst Jayson = require('jayson');\n\n// Declare an object literal with all the methods you want to expose...\nconst methods =\n{\n    foo: () =\u003e 'hello',\n    bar: (a, b) =\u003e a + b,\n    baz: ({name}) =\u003e 'hello ' + name \n};\n\n// ...or pass in an instance of a class with the methods you want to expose.\nclass Method\n{\n    foo()\n    {\n        return 'hello';\n    }\n\n    bar(a, b)\n    {\n        return a + b\n    }\n\n    baz({name})\n    {\n        return 'hello ' + name;\n    }\n}\n\nconst methods = new Method();\n```\n\n**Note: The Jayson server passes a `context` object argument to every method that's called. If the `params` property of a `request` is an object then the property `context` will be added to that object. If the `params` property is any other type then the first argument passed to the method will be the `context` object.**\n\n`context` {Object} Method context.\n* `headers` {Object} Request headers.\n* `auth` {String} A [JWT](https://github.com/auth0/node-jsonwebtoken) providing the authentication context.\n\nDefine your config (default values shown below):\n\n```javascript\nconst config =\n{\n    title: 'Jayson Server API',\n    methods,\n    logger: false,\n    jsonLimit: '1mb',\n    timeout: 60000,\n    http:\n    {\n        port: 3000,\n        cors: {},\n        helmet: {noCache: true},\n        compress: {}\n    },\n    ws:\n    {\n        port: 3001,\n        heartbeat: 30000\n    },\n    jwt:\n    {\n        secret: 'sauce'\n    }\n}\n```\n* `title` {String} Name of the API instance. Default = `'Jayson Server API'`.\n* `description` {String} Description of the API instance. Default = `undefined`.\n* `$id` {String} [JSON Schema ID](https://spacetelescope.github.io/understanding-json-schema/structuring.html#the-id-property). Default = `undefined`.\n* `methods` {Object} **(Required)** Object containing the methods exposed to the RPC server. Default = `undefined`.\n* `definitions` {Object} Schema definitions. Use this when you need to reference shared definitions from method schemas. See the [schema-definitions](https://github.com/mhingston/jayson/blob/master/examples/schema-definitions) example. Default = `undefined`.\n* `logger` {Boolean|Function} Set to true to have debug log written to the console or pass in a function to receive the log messages. Default = `undefined`.\n* `jsonLimit` {String} Maximum size of the message payload. Default = `'1mb'`.\n* `timeout` {Number|Null} Default timeout for all RPC calls (in milliseconds). Set to `null` to disable default timeout. Default = `60000`.\n* `http` {Object}. Default = `undefined`.\n  * `port` {Number} Port to listen to HTTP connections on. Default = `3000`.\n  * `cors` {Object} CORS options, see [koa2-cors](https://github.com/zadzbw/koa2-cors#options). Default = `{}`.\n  * `helmet` {Object} Helmet options, see [koa-helmet](https://github.com/venables/koa-helmet). Default = `{noCache: true}`.\n  * `compress` {Object} Compress options, see [compress](https://github.com/koajs/compress#options). Default = `{}`. \n* `ws` {Object}. Default = `undefined`.\n  * `port` {Number} Port to listen to WebSocket connections on. Default = `3001`.\n  * `heartbeat` {Number} How often to send pings to clients (in milliseconds). Default = `30000`.\n* `electron` {Boolean} Whether the server is running in electron. Default = `undefined`.\n* `jwt` {Object}. Default = `{}`.\n  * `secret` {String|Buffer|Object} See [jwt.sign](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback). Default = `'sauce'`.\n  * `options` {Object} See [jwt.sign](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback).\n\n**Note: The config must include a `http` and/or a `ws` property unless you're using electron in which case just set `electron: true`.**\n\nInstantiate a new RPC server:\n```javascript\nnew Jayson.Server(config);\n```\n\n## Client Usage\n\nFor use in a browser you can either include the bundle [`dist/jayson.min.js`](https://github.com/mhingston/jayson/blob/master/dist/jayson.min.js) or you can import the module using a module loader.\n\n**Note: When used in a browser the global variable `window.Jayson` is set.**\n\n```javascript\n// Import the module\nconst Jayson = require('jayson');\n```\n\nDefine your config (default values shown below):\n\n```javascript\nconst config =\n{\n    retryDelay: 3000,\n    timeout: 60000,\n    logger: false,\n    url: 'http://127.0.0.1:3000'\n}\n```\n* `retryDelay` {Number} If the connection to the WebSocket server is lost how often should the client attempt to reconnect (in milliseconds). Default = `3000`.\n* `timeout` {Number} How long to wait for a response for every RPC call (in milliseconds). Default = `60000`.\n* `logger` {Boolean|Function} Set to true to have debug log written to the console or pass in a function to receive the log messages. Default = `undefined`.\n* `url` {String} The URL of the Jayson server. To connect to a WebSocket server use a WebSocket protocol i.e. `ws://` or `wss://`. If you're using electron this isn't required. Default = `undefined`.\n* `electron` {Boolean} Whether the client is running in electron. Default = `undefined`.\n\nInstantiate a new RPC client:\n\n```javascript\nconst client = new Jayson.Client(config);\n```\n\n### Class: Client\n\n#### client.connect(callback) [async]\nConnect to the RPC server.\n\n* `callback(error, client)` {Function} Callback function (optional).\n  * `error` {Object|Null} Error object.\n  * `client` {Object} The client instance.\n\n#### client.discover() [async]\nRetrieve the RPC methods schema from the RPC server. This is necessary to validate future RPC calls. If you don't call this method then schema validation will be disabled.\n\n* `callback(error, result)` {Function} Callback function.\n  * `error` {Object|Null} Error object.\n  * `result` {Object} Method schema.\n\n#### client.call(args) [async]\n\nCall a method on the RPC server. \n\n* `args` {Object|Array`\u003cObject\u003e`}.\n  * `method` {String} **(Required)** Name of the RPC method to call.\n  * `params` {Array|Object} Arguments to pass to the RPC method. Be aware that your params will be serialized as JSON (i.e. [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)).\n  * `auth` {String} A [JWT](https://github.com/auth0/node-jsonwebtoken) providing the authentication context.\n  * `timeout` {Number} How long to wait for a response for the RPC call (in milliseconds).\n  * `notification` {Boolean} Whether the call is a notification or not (i.e. expects a response).\n  * `callback` {Function} Callback function.\n    * `error` {Object} Error object.\n    * `result` {String|Number|Boolean|Null|Undefined|Array|Object} Result from the RPC call.\n\n### Example\n\n```javascript\nclient.connect()\n.then(() =\u003e client.discover())\n.then(() =\u003e client.call(\n{\n    method: 'foo'\n}))\n.then(response =\u003e\n{\n    console.log(response);\n})\n.catch(error =\u003e\n{\n    console.log(error.message);\n})\n```\n\nSee the [examples](https://github.com/mhingston/jayson/blob/master/examples) folder for more.\n\n## Generating Documentation\n\nOnce you have an API server up and running and have provided a schema for some/all of your methods you can generate a HTML file using [docson](https://github.com/TexKiller/node-docson).\n\nFrom your project directory:\n\n`node node_modules/.bin/docson --server \u003cServer URL\u003e --output \u003cOutput File\u003e`\n\ne.g.\n\n`node node_modules/.bin/docson --server http://127.0.0.1:3000 --output index.html`\n\n## Notes\n\n* Jayson supports calling async methods, i.e. functions returning a promise.\n* Rate limiting requests is not supported and is beyond the scope of this project. It's better handled by a reverse proxy and/or firewall.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhingston%2Fjayson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmhingston%2Fjayson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhingston%2Fjayson/lists"}