{"id":16370279,"url":"https://github.com/delvedor/rinvoke","last_synced_at":"2025-06-20T15:34:53.380Z","repository":{"id":66171900,"uuid":"93240209","full_name":"delvedor/rinvoke","owner":"delvedor","description":"RPC library based on net sockets, can work both with tcp sockets and ipc.","archived":false,"fork":false,"pushed_at":"2017-08-20T09:44:22.000Z","size":53,"stargazers_count":12,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-29T09:55:59.242Z","etag":null,"topics":["ipc","multiplex","nodejs","rpc","socket"],"latest_commit_sha":null,"homepage":"","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/delvedor.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}},"created_at":"2017-06-03T10:08:36.000Z","updated_at":"2021-10-05T14:31:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"df373f1b-8759-4024-9b16-4ad761874b0c","html_url":"https://github.com/delvedor/rinvoke","commit_stats":{"total_commits":70,"total_committers":2,"mean_commits":35.0,"dds":"0.014285714285714235","last_synced_commit":"fac7e4868a4159dd2fd24f7b66540cd771138ffb"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/delvedor/rinvoke","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delvedor%2Frinvoke","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delvedor%2Frinvoke/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delvedor%2Frinvoke/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delvedor%2Frinvoke/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/delvedor","download_url":"https://codeload.github.com/delvedor/rinvoke/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delvedor%2Frinvoke/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260971182,"owners_count":23090691,"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":["ipc","multiplex","nodejs","rpc","socket"],"created_at":"2024-10-11T03:04:32.810Z","updated_at":"2025-06-20T15:34:48.365Z","avatar_url":"https://github.com/delvedor.png","language":"JavaScript","readme":"# rinvoke\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)\n  [![Build Status](https://travis-ci.org/delvedor/rinvoke.svg?branch=master)](https://travis-ci.org/delvedor/rinvoke)  [![Coverage Status](https://coveralls.io/repos/github/delvedor/rinvoke/badge.svg?branch=master)](https://coveralls.io/github/delvedor/rinvoke?branch=master)\n\nBuild your distributed functions system with **Rinvoke**!  \n**Rinvoke** is a RPC library based on net sockets, can work both with **tcp sockets** and **ipc**.  \nIt has built in reconnect logic and supports multiple parser/serializers, such as [msgpack](http://msgpack.org/) or [protbuf](https://developers.google.com/protocol-buffers/).  \nInternally uses [tentacoli](https://github.com/mcollina/tentacoli) to multiplex the requests and [avvio](https://github.com/mcollina/avvio) to guarantee the asynchronous bootstrap of the application, it also provide an handy request validation out of the box with [*JSON schema*](http://json-schema.org/).\n\n\u003ca name=\"install\"\u003e\u003c/a\u003e\n## Install\n```\nnpm i rinvoke --save\n```\n\n\u003ca name=\"usage\"\u003e\u003c/a\u003e\n## Usage\nRinvoke could be used as a server or client, so when your require it you must specify it.  \nLet's see an example for the server:\n```js\nconst rinvoke = require('rinvoke/server')()\n\nrinvoke.register('concat', (req, reply) =\u003e {\n  reply(null, req.a + req.b)\n})\n\nrinvoke.listen(3000, err =\u003e {\n  if (err) throw err\n})\n```\nAnd now for the client:\n```js\nconst rinvoke = require('rinvoke/client')({\n  port: 3000\n})\n\nrinvoke.invoke({\n  procedure: 'concat',\n  a: 'hello ',\n  b: 'world'\n}, (err, result) =\u003e {\n  if (err) {\n    console.log(err)\n    return\n  }\n  console.log(result)\n})\n```\nThe client could seem synchronous but internally everything is handled asynchronously with events.  \nCheckout the [examples folder](https://github.com/delvedor/rinvoke/tree/master/examples) if you want to see more examples!\n\n\u003ca name=\"api\"\u003e\u003c/a\u003e\n## API\n\n\u003ca name=\"server\"\u003e\u003c/a\u003e\n### Server\n#### `server([opts])`\nInstance a new server, the options object can accept a custom parser/serializer via the `codec` field.\n```js\nconst rinvoke = require('rinvoke/server')({\n  codec: {\n    encode: JSON.stringify,\n    decode: JSON.parse\n  }\n})\n```\nThe default codec is *JSON*.  \n**Events**:\n- `'connection'`\n- `'error'`\n- `'request'`\n- `'listening'`\n\n#### `register(procedureName, [schema,] procedureFunction)`\nRegisters a new procedure, the name of the procedure must be a string, the function has the following signature: `(request, reply)` where `request` is the request object and `reply` a function t send the response back to the client.\n```js\nrinvoke.register('concat', (req, reply) =\u003e {\n  reply(null, req.a + req.b)\n})\n```\n*Promises* and *async/await* are supported as well!\n```js\nrinvoke.register('concat', async req =\u003e {\n  return req.a + req.b\n})\n```\n\u003ca name=\"validation\"\u003e\u003c/a\u003e\n#### Validation\nRinvoke offers you out of the box a nice and standard way to validate your requests, [*JSON schema*](http://json-schema.org/)!  \nInternally uses [ajv](https://github.com/epoberezkin/ajv/blob/master/README.md) to achieve the maximum speed and correctness.\n```js\nrinvoke.register('concat', {\n  type: 'object',\n  properties: {\n    a: { type: 'string' },\n    b: { type: 'string' }\n  },\n  required: ['a', 'b']\n}, (req, reply) =\u003e {\n  reply(null, req.a + req.b)\n})\n```\n\n#### `listen(portOrPath, [address], callback)`\nRun the server over the specified `port` (and `address`, default to `127.0.0.1`), if you specify a path (as a string)\n it will use the system socket to perform ipc.\n ```js\n rinvoke.listen(3000, err =\u003e {\n   if (err) throw err\n })\n\n rinvoke.listen(3000, '127.0.0.1', err =\u003e {\n   if (err) throw err\n })\n\n rinvoke.listen('/tmp/socket.sock', err =\u003e {\n   if (err) throw err\n })\n ```\n\n\u003ca name=\"client\"\u003e\u003c/a\u003e\n### Client\n\n#### `client(options)`\nInstance a new client, the options object must contain a `port` or `path` field, furthermore can accept a custom parser/serializer via the `codec` field. If you want to activate the automatic reconnection handling pass `reconnect: true` (3 attempts with 1s timeout), if you want to configure the timeout handling pass an object like the following:\n```js\nconst rinvoke = require('rinvoke/client')({\n  port: 3000,\n  address: '127.0.0.1'\n  reconnect: {\n    attempts: 5,\n    timeout: 2000\n  },\n  codec: {\n    encode: JSON.stringify,\n    decode: JSON.parse\n  }\n})\n```\nThe default codec is *JSON*.  \n**Events**:\n- `'connect'`\n- `'error'`\n- `'close'`\n- `'timeout'`\n\n#### `invoke(request, callback)`\nInvoke a procedure on the server, the request object **must** contain the key `procedure` with the name of the function to call.  \nThe callback is a function with the following signature: `(error, response)`.\n```js\nrinvoke.invoke({\n  procedure: 'concat',\n  a: 'hello ',\n  b: 'world'\n}, (err, result) =\u003e {\n  if (err) {\n    console.log(err)\n    return\n  }\n  console.log(result)\n})\n```\n*Promises* are supported as well!\n```js\nrinvoke\n  .invoke({\n    procedure: 'concat',\n    a: 'a',\n    b: 'b'\n  })\n  .then(console.log)\n  .catch(console.log)\n```\n\n#### `fire(request [, callback])`\nFire (and forget) a procedure on the server, the request object **must** contain the key `procedure` with the name of the function to call.  \nThe optional callback will be called if there is an error while sending the message, or after the message has been sent successfully.\n\n```js\nrinvoke.fire({\n  procedure: 'concat',\n  a: 'hello ',\n  b: 'world'\n})\n```\n\n#### `timeout(time)`\nSets the timeout of the socket.\n#### `keepAlive(bool)`\nSets the `keep-alive` property.\n\n\u003ca name=\"shared\"\u003e\u003c/a\u003e\n### Method for both client and server\n#### `use(callback)`\nThe callback is a function witb the following signature: `instance, options, next`.  \nWhere `instance` is the client instance, options, is an options object and  `next` a function you must call when your code is ready.  \nThis api is useful if you need to load an utility, a database connection for example. `use` will guarantee the load order an that your client/server will boot up once every `use` has completed.\n```js\nrinvoke.use((instance, opts, next) =\u003e {\n  dbClient.connect(opts.url, (err, conn) =\u003e {\n    instance.db = conn // now you can access in your function the database connection with `this.db`\n    next()\n  })\n})\n```\n#### `onClose(callback)`\nHook that will be called once you fire the `close` callback.\n```js\nrinvoke.onClose((instance, done) =\u003e {\n  // do something\n  done()\n})\n```\n\n#### `close(callback)`\nOnce you call this function the socket server and client will close and all the registered functions with `onClose` will be called.\n```js\nrinvoke.close((err, instance, done) =\u003e {\n  // do something\n  done()\n})\n```\n\u003ca name=\"cli\"\u003e\u003c/a\u003e\n### CLI\nYou can even run the server with the integrated cli!\nIn your `package.json` add:\n```json\n{\n  \"scripts\": {\n    \"start\": \"rinvoke server.js\"\n  }\n}\n```\nAnd then create your server file:\n```js\nmodule.exports = async req =\u003e `Hello ${req.name}!`\n```\nYou can also use an extended version of the above example:\n```js\nfunction sayHello (rinvoke, opts, next) {\n  rinvoke.register('hello', (req, reply) =\u003e {\n    reply(null, { hello: 'world' })\n  })\n\n  next()\n}\n\nmodule.exports = sayHello\n```\nThe options of the cli are:\n```\n--port       -p      # default 3000\n--address    -a      # default 127.0.0.1\n--path       -P      # path of the ipc web socket\n--name       -n      # name of your exported function\n```\n\n\u003ca name=\"acknowledgements\"\u003e\u003c/a\u003e\n## Acknowledgements\n\nThis project is kindly sponsored by [LetzDoIt](http://www.letzdoitapp.com/).\n\n\u003ca name=\"license\"\u003e\u003c/a\u003e\n## License\n\n[MIT](./LICENSE)\n\nCopyright © 2017 Tomas Della Vedova\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdelvedor%2Frinvoke","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdelvedor%2Frinvoke","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdelvedor%2Frinvoke/lists"}