{"id":18487530,"url":"https://github.com/pocesar/node-jsonrpc2","last_synced_at":"2026-01-10T22:53:56.959Z","repository":{"id":57111626,"uuid":"11519794","full_name":"pocesar/node-jsonrpc2","owner":"pocesar","description":"JSON-RPC 2.0 server and client library, with HTTP (with Websocket support) and TCP endpoints","archived":false,"fork":true,"pushed_at":"2020-04-18T01:12:26.000Z","size":223,"stargazers_count":105,"open_issues_count":22,"forks_count":40,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-01T10:37:51.164Z","etag":null,"topics":["http","http-server","https","https-server","json-rpc","json-rpc2","jsonrpc2","nodejs","tcp","websocket","websocket-server","websockets"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"fuson/node-jsonrpc2","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pocesar.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}},"created_at":"2013-07-19T04:03:58.000Z","updated_at":"2024-07-07T14:29:42.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/pocesar/node-jsonrpc2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocesar%2Fnode-jsonrpc2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocesar%2Fnode-jsonrpc2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocesar%2Fnode-jsonrpc2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pocesar%2Fnode-jsonrpc2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pocesar","download_url":"https://codeload.github.com/pocesar/node-jsonrpc2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247922930,"owners_count":21018893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","http-server","https","https-server","json-rpc","json-rpc2","jsonrpc2","nodejs","tcp","websocket","websocket-server","websockets"],"created_at":"2024-11-06T12:50:31.751Z","updated_at":"2026-01-10T22:53:56.916Z","avatar_url":"https://github.com/pocesar.png","language":"JavaScript","readme":"[![Build Status](https://travis-ci.org/pocesar/node-jsonrpc2.svg?branch=master)](https://travis-ci.org/pocesar/node-jsonrpc2)\r\n\r\n[![NPM](https://nodei.co/npm/json-rpc2.svg?downloads=true)](https://nodei.co/npm/json-rpc2/)\r\n\r\n# node-jsonrpc2\r\n\r\nJSON-RPC 2.0 server and client library, with `HTTP` (with `Websocket` support) and `TCP` endpoints\r\n\r\nThis fork is a rewrite with proper testing framework, linted code, compatible with node 0.8.x and 0.10.x, class inheritance, and added functionalities\r\n\r\n## Tools\r\n\r\nCheck [jsonrpc2-tools](https://www.npmjs.org/package/jsonrpc2-tools) for some nice additions to this module.\r\n\r\n## Install\r\n\r\nTo install node-jsonrpc2 in the current directory, run:\r\n\r\n```bash\r\nnpm install json-rpc2 --save\r\n```\r\n\r\n## Changes from 1.x\r\n\r\n* Uses native EventEmitter instead of EventEmitter3\r\n\r\n## Changes from 0.x\r\n\r\n* Before, the `id` member was permissive and wouldn't actually adhere to the RFC, allowing anything besides `undefined`.\r\n* If your application relied on weird id constructs other than `String`, `Number` or `null`, it might break if you update to 1.x\r\n\r\n## Usage\r\n\r\nFiring up an efficient JSON-RPC server becomes extremely simple:\r\n\r\n```js\r\nvar rpc = require('json-rpc2');\r\n\r\nvar server = rpc.Server.$create({\r\n    'websocket': true, // is true by default\r\n    'headers': { // allow custom headers is empty by default\r\n        'Access-Control-Allow-Origin': '*'\r\n    }\r\n});\r\n\r\nfunction add(args, opt, callback) {\r\n  callback(null, args[0] + args[1]);\r\n}\r\n\r\nserver.expose('add', add);\r\n\r\n// you can expose an entire object as well:\r\n\r\nserver.expose('namespace', {\r\n    'function1': function(){},\r\n    'function2': function(){},\r\n    'function3': function(){}\r\n});\r\n// expects calls to be namespace.function1, namespace.function2 and namespace.function3\r\n\r\n// listen creates an HTTP server on localhost only\r\nserver.listen(8000, 'localhost');\r\n```\r\n\r\nAnd creating a client to speak to that server is easy too:\r\n\r\n```js\r\nvar rpc = require('json-rpc2');\r\n\r\nvar client = rpc.Client.$create(8000, 'localhost');\r\n\r\n// Call add function on the server\r\n\r\nclient.call('add', [1, 2], function(err, result) {\r\n    console.log('1 + 2 = ' + result);\r\n});\r\n```\r\n\r\nCreate a raw (socket) server using:\r\n\r\n```js\r\nvar rpc = require('json-rpc2');\r\n\r\nvar server = rpc.Server.$create();\r\n\r\n// non-standard auth for RPC, when using this module using both client and server, works out-of-the-box\r\nserver.enableAuth('user', 'pass');\r\n\r\n// Listen on socket\r\nserver.listenRaw(8080, 'localhost');\r\n```\r\n\r\n## Extend, overwrite, overload\r\n\r\nAny class can be extended, or used as a mixin for new classes, since it uses [ES5Class](http://github.com/pocesar/ES5-Class) module.\r\n\r\nFor example, you may extend the `Endpoint` class, that automatically extends `Client` and `Server` classes.\r\nExtending `Connection` automatically extends `SocketConnection` and `HttpServerConnection`.\r\n\r\n```js\r\nvar rpc = require('json-rpc2');\r\n\r\nrpc.Endpoint.$include({\r\n    'newFunction': function(){\r\n\r\n    }\r\n});\r\n\r\nvar\r\n    server = rpc.Server.$create(),\r\n    client = rpc.Client.$create();\r\n\r\nserver.newFunction(); // already available\r\nclient.newFunction(); // already available\r\n```\r\n\r\nTo implement a new class method (that can be called without an instance, like `rpc.Endpoint.newFunction`):\r\n\r\n```js\r\nvar rpc = require('json-rpc2');\r\n\r\nrpc.Endpoint.$implement({\r\n    'newFunction': function(){\r\n    }\r\n});\r\n\r\nrpc.Endpoint.newFunction(); // available\r\nrpc.Client.newFunction(); // every\r\nrpc.Server.newFunction(); // where\r\n```\r\n\r\nDon't forget, when you are overloading an existing function, you can call the original function using `$super`\r\n\r\n```js\r\nvar rpc = require('json-rpc2');\r\n\r\nrpc.Endpoint.$implement({\r\n    'trace': function($super, direction, message){\r\n        $super(' (' + direction + ')', message); //call the last defined function\r\n    }\r\n});\r\n```\r\n\r\nAnd you can start your classes directly from any of the classes\r\n\r\n```js\r\nvar MyCoolServer = require('json-rpc2').Server.$define('MyCoolServer', {\r\n    myOwnFunction: function(){\r\n    },\r\n}, {\r\n    myOwnClassMethod: function(){\r\n    }\r\n}); // MyCoolServer will contain all class and instance functions from Server\r\n\r\nMyCoolServer.myOwnClassMethod(); // class function\r\nMyCoolServer.$create().myOwnFunction(); // instance function\r\n```\r\n\r\n## Debugging\r\n\r\nThis module uses the [debug](http://github.com/visionmedia/debug) package, to debug it, you need to set the Node\r\nenvironment variable to jsonrpc, by setting it in command line as `set DEBUG=jsonrpc` or `export DEBUG=jsonrpc`\r\n\r\n## Examples\r\n\r\nTo learn more, see the `examples` directory, peruse `test/jsonrpc-test.js`, or\r\nsimply \"Use The Source, Luke\".\r\n\r\nMore documentation and development is on its way.\r\n\r\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpocesar%2Fnode-jsonrpc2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpocesar%2Fnode-jsonrpc2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpocesar%2Fnode-jsonrpc2/lists"}