{"id":13686687,"url":"https://github.com/fibjs/fib-rpc","last_synced_at":"2026-01-20T16:41:46.824Z","repository":{"id":57235129,"uuid":"86342149","full_name":"fibjs/fib-rpc","owner":"fibjs","description":"Remote Procedure Calling for fibjs","archived":false,"fork":false,"pushed_at":"2019-07-30T18:54:20.000Z","size":87,"stargazers_count":0,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-01T09:39:03.935Z","etag":null,"topics":["rpc","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fibjs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-27T13:58:11.000Z","updated_at":"2020-08-10T15:29:13.000Z","dependencies_parsed_at":"2022-08-23T15:50:47.106Z","dependency_job_id":null,"html_url":"https://github.com/fibjs/fib-rpc","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/fibjs/fib-rpc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fibjs","download_url":"https://codeload.github.com/fibjs/fib-rpc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibjs%2Ffib-rpc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259462578,"owners_count":22861514,"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":["rpc","websocket"],"created_at":"2024-08-02T15:00:37.916Z","updated_at":"2026-01-20T16:41:46.818Z","avatar_url":"https://github.com/fibjs.png","language":"JavaScript","readme":"# fib-rpc\n\n[![NPM version](https://img.shields.io/npm/v/fib-rpc.svg)](https://www.npmjs.org/package/fib-rpc)\n[![Build Status](https://travis-ci.org/fibjs/fib-rpc.svg)](https://travis-ci.org/fibjs/fib-rpc)\n[![Build status](https://ci.appveyor.com/api/projects/status/kqvsr2po0j0enlu1?svg=true)](https://ci.appveyor.com/project/richardo2016/fib-rpc)\n\nRemote Procedure Calling for fibjs\n\n## Introduction\n\n`fib-rpc` is working on dispatching of remote procedure calling, it supports both http request and websocket(**recommended**).\n\nobviously, it follows [JSON-RPC] protocol.\n\n## Get Started\n\n```\nnpm i -S fib-rpc\n```\n\n## Usage\n\nyou can use it with `http.Request`\n```javascript\nvar js_remote = rpc.handler({\n    foo: function (p1, p2) {\n        return p1 + ',' + p2;\n    }\n});\n\nfunction js_call(r) {\n    var m = new http.Request();\n\n    m.value = 'test/tttt/tttt/';\n    m.json(r);\n\n    js_remote(m);\n\n    m.response.body.rewind();\n    return m.response.readAll().toString();\n}\n\nvar result = js_call({\n    // method is required\n    method: 'foo',\n    // params is required and must be array\n    params: [100, 200],\n    // id is required\n    id: 1234\n})\n\nassert.equal(result, '{\"id\":1234,\"result\":\"100,200\"}');\n```\n\nbut in most cases, you may prefer using it based on `Websocket`, which finshied by `rpc.connect`\n```javascript\nconst ws = require('ws')\nconst http = require('http')\n\nconst rpc = require('fib-rpc')\n\nconst svr = new http.Server(8811, ws.upgrade(\n    rpc.handler(\n        {\n            test: function (v1, v2) {\n                return v1 + v2;\n            }\n        }\n    ))\n);\nsvr.asyncRun();\n\nconst remoting = rpc.connect(\"ws://127.0.0.1:8811\");\n\nremoting.test(1, 2) // 3\n```\n\nLearn `connect` from test case `'websocket rpc'` in [exmaple](./examples/connect.js).\n\n### Custom Errors\n\nuse `RpcError` as typed Error thrown if required:\n\n```javascript\nconst rpc = require('fib-rpc')\n\nconst js_remote = rpc.handler(\n    {\n        integerAdd: function (v1, v2) {\n            if (!Number.isInteger(v1) || !Number.isInteger(v2))\n                throw rpc.rpcError(4010000, 'Addend must be integer')\n                \n            return v1 + v2;\n        }\n    }\n);\n\nconst svr = new http.Server(8811, ws.upgrade(js_remote));\n\nconst remoting = rpc.connect(\"ws://127.0.0.1:8811\");\n\ntry {\n    remoting.integerAdd(1.1, 2)\n} catch (err_msg) {\n    console.log(err_msg) // 'Addend must be integer'\n}\n\nconsole.log(\n    js_call({\n        method: 'foo',\n        params: [1.1, 2],\n        id: 1234\n    })\n) // code: 4010000, message: 'Addend must be integer'\n```\n\n## Samples\n\nView [Samples](./examples), test them like this:\n```bash\n# build lib first.\nnpm i \u0026\u0026 npm run build\n\n# chdir to examples\ncd exapmles\nnpm i\n\n# run example\nfibjs ./connect.js\nfibjs ./open_handler-js.js\n```\n\n[test.js]:test.js#L123:1\n\n[JSON-RPC]:https://www.jsonrpc.org/specification#overview","funding_links":[],"categories":["Packages"],"sub_categories":["Server App"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs%2Ffib-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffibjs%2Ffib-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibjs%2Ffib-rpc/lists"}