{"id":22608914,"url":"https://github.com/bitfinexcom/grenache-nodejs-http","last_synced_at":"2025-04-11T06:14:01.893Z","repository":{"id":56415865,"uuid":"63519741","full_name":"bitfinexcom/grenache-nodejs-http","owner":"bitfinexcom","description":"Grenache Node.JS WebSocket implementation","archived":false,"fork":false,"pushed_at":"2023-03-18T09:09:08.000Z","size":935,"stargazers_count":13,"open_issues_count":1,"forks_count":15,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-11T06:13:56.428Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.bitfinex.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bitfinexcom.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-17T07:21:48.000Z","updated_at":"2025-01-15T14:21:53.000Z","dependencies_parsed_at":"2024-06-19T00:06:31.505Z","dependency_job_id":"d552c198-597e-4e06-b140-d491ebc82155","html_url":"https://github.com/bitfinexcom/grenache-nodejs-http","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/bitfinexcom%2Fgrenache-nodejs-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitfinexcom%2Fgrenache-nodejs-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitfinexcom%2Fgrenache-nodejs-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitfinexcom%2Fgrenache-nodejs-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitfinexcom","download_url":"https://codeload.github.com/bitfinexcom/grenache-nodejs-http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248351393,"owners_count":21089270,"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":[],"created_at":"2024-12-08T15:10:06.047Z","updated_at":"2025-04-11T06:14:01.870Z","avatar_url":"https://github.com/bitfinexcom.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [Grenache](https://github.com/bitfinexcom/grenache) Node.JS HTTP implementation\n\n\u003cimg src=\"logo.png\" width=\"15%\" /\u003e\n\nGrenache is a micro-framework for connecting microservices. Its simple and optimized for performance.\n\nInternally, Grenache uses Distributed Hash Tables (DHT, known from Bittorrent) for Peer to Peer connections. You can find more details how Grenche internally works at the [Main Project Homepage](https://github.com/bitfinexcom/grenache)\n\n - [Setup](#setup)\n - [Examples](#examples)\n - [API](#api)\n\n## Setup\n\n### Install\n```\nnpm install --save grenache-nodejs-http\n```\n\n### Other Requirements\n\nInstall `Grenache Grape`: https://github.com/bitfinexcom/grenache-grape:\n\n```bash\nnpm i -g grenache-grape\n```\n\n```\n// Start 2 Grapes\ngrape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'\ngrape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'\n```\n\n### Examples\n\n#### RPC Server / Client\n\nThis RPC Server example announces a service called `rpc_test`\non the overlay network. When a request from a client is received,\nit replies with `world`. It receives the payload `hello` from the\nclient.\n\nThe client sends `hello` and receives `world` from the server.\n\nInternally the DHT is asked for the IP of the server and then the\nrequest is done as Peer-to-Peer request via websockets.\n\n**Grape:**\n\n```bash\ngrape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'\ngrape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'\n```\n\n**Server:**\n\n```js\nconst Link = require('grenache-nodejs-link')\n\nconst link = new Link({\n  grape: 'http://127.0.0.1:30001'\n})\nlink.start()\n\nconst peer = new PeerRPCServer(link, {\n  timeout: 300000\n})\npeer.init()\n\nconst service = peer.transport('server')\nservice.listen(_.random(1000) + 1024)\n\nsetInterval(function () {\n  link.announce('rpc_test', service.port, {})\n}, 1000)\n\nservice.on('request', (rid, key, payload, handler) =\u003e {\n  console.log(payload) // hello\n  handler.reply(null, 'world')\n})\n```\n\n**Client:**\n\n```js\nconst Link = require('grenache-nodejs-link')\n\nconst link = new Link({\n  grape: 'http://127.0.0.1:30001'\n})\nlink.start()\n\nconst peer = new PeerRPCClient(link, {})\npeer.init()\n\npeer.request('rpc_test', 'hello', { timeout: 10000 }, (err, data) =\u003e {\n  if (err) {\n    console.error(err)\n    process.exit(-1)\n  }\n  console.log(data) // world\n})\n```\n\n[Code Server](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_server.js)\n[Code Client](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_client.js)\n\n## API\n\n### Class: PeerRPCServer\n\n#### Event: 'stream'\n\nAlways emitted as son as a request arrives. Emits the raw `req` and `res` streams\nof the request and some preparsed metadata. Used for streaming. If\n`disableBuffered` is set to `false`, the server will attempt to buffer after\nemitting the stream event.\n\n```js\nserviceStr.on('stream', (req, res, meta, handler) =\u003e {\n  console.log(meta) // meta.isStream === true\n\n  const [rid, key] = meta.infoHeaders\n\n  req.pipe(process.stdout)\n\n  handler.reply(rid, null, 'world') // convenience reply\n})\n\n```\n\n[Example](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_server_stream.js).\n\n\n\n#### Event: 'request'\n\nEmitted when a request from a RPC client is received. In the lifecycle of a\nrequest this happens after the server has parsed an buffered the whole data.\nWhen the server runs with `disableBuffered: true`, the event must emitted manually,\nif needed, or by calling the buffering request handlers manually.\n\n  - `rid` unique request id\n  - `key` name of the service\n  - `payload` Payload sent by client\n  - `handler` Handler object, used to reply to a client.\n\n```js\nservice.on('request', (rid, key, payload, handler) =\u003e {\n  handler.reply(null, 'world')\n})\n```\n\n#### new PeerRPCServer(link, [options])\n\n - `link \u003cObject\u003e` Instance of a [Link Class](#new-linkoptions)\n - `options \u003cObject\u003e`\n   - `disableBuffered \u003cBoolean\u003e` Disable automatic buffering of the incoming request data stream. Useful for streaming.\n   - `timeout \u003cObject\u003e` Server-side socket timeout\n   - `secure \u003cObject\u003e` TLS options\n     - `key \u003cBuffer\u003e`\n     - `cert \u003cBuffer\u003e`\n     - `ca \u003cBuffer\u003e`\n     - `requestCert \u003cBoolean\u003e`\n     - `rejectUnauthorized \u003cBoolean\u003e`\n\nCreates a new instance of a `PeerRPCServer`, which connects to the DHT\nusing the passed `link`.\n\n#### peer.init()\n\nSets the peer active. Must get called before we get a transport\nto set up a server.\n\n#### peer.transport('server')\n\nMust get called after the peer is active. Sets peer into server-\nmode.\n\n#### peer.listen(port)\n\nLets the `PeerRPCServer` listen on the desired `port`. The port is\nstored in the DHT.\n\n#### peer.port\n\nPort of the server (set by `listen(port)`).\n\n#### Example\n\nThis RPC Server example announces a service called `rpc_test`\non the overlay network. When a request from a client is received,\nit replies with `world`. It receives the payload `hello` from the\nclient.\n\nThe client sends `hello` and receives `world` from the server.\n\nInternally the DHT is asked for the IP of the server and then the\nrequest is done as Peer-to-Peer request via websockets.\n\n**Server:**\n\n```js\nconst Link = require('grenache-nodejs-link')\n\nconst link = new Link({\n  grape: 'http://127.0.0.1:30001'\n})\nlink.start()\n\nconst peer = new PeerRPCServer(link, {})\npeer.init()\n\nconst service = peer.transport('server')\nservice.listen(_.random(1000) + 1024)\n\nsetInterval(function () {\n  link.announce('rpc_test', service.port, {})\n}, 1000)\n\nservice.on('request', (rid, key, payload, handler) =\u003e {\n  console.log(payload) // hello\n  handler.reply(null, 'world')\n})\n```\n\n**Client:**\n\n```js\nconst Link = require('grenache-nodejs-link')\n\nconst link = new Link({\n  grape: 'http://127.0.0.1:30001'\n})\nlink.start()\n\nconst peer = new PeerRPCClient(link, {})\npeer.init()\n\npeer.request('rpc_test', 'hello', { timeout: 10000 }, (err, data) =\u003e {\n  if (err) {\n    console.error(err)\n    process.exit(-1)\n  }\n  console.log(data) // world\n})\n```\n\n[Server](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_server.js)\n[Client](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_client.js)\n\n\n### Class: PeerRPCClient\n\n#### new PeerRPCClient(link, [options])\n\n - `link \u003cObject\u003e` Instance of a [Link Class](#new-linkoptions)\n - `options \u003cObject\u003e`\n   - `maxActiveKeyDests \u003cNumber\u003e`\n   - `maxActiveDestTransports \u003cNumber\u003e`\n   - `secure \u003cObject\u003e` TLS options\n     - `key \u003cBuffer\u003e`\n     - `cert \u003cBuffer\u003e`\n     - `ca \u003cBuffer\u003e`\n     - `rejectUnauthorized \u003cBoolean\u003e`\n\n\nCreates a new instance of a `PeerRPCClient`, which connects to the DHT\nusing the passed `link`.\n\nA PeerRPCClient can communicate with multiple Servers and map work items over them.\nWith `maxActiveKeyDests` you can limit the maximum amount of destinations.\nAdditionally, you can limit the amount of transports with `maxActiveDestTransports`.\n\n#### peer.init()\n\nSets the peer active. Must get called before we start to make requests.\n\n#### peer.map(name, payload, [options], callback)\n  - `name \u003cString\u003e` Name of the service to address\n  - `payload \u003cString\u003e` Payload to send\n  - `options \u003cObject\u003e` Options for the request\n    - `timeout \u003cNumber\u003e` timeout in ms\n    - `limit \u003cNumber\u003e` maximum requests per available worker\n  - `callback \u003cFunction\u003e`\n\nMaps a number of requests over the amount of registered workers / PeerRPCServers.\n[Example](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_client_map.js).\n\n\n#### peer.request(name, payload, [options], callback)\n  - `name \u003cString\u003e` Name of the service to address\n  - `payload \u003cString\u003e` Payload to send\n  - `options \u003cObject\u003e` Options for the request\n    - `timeout \u003cNumber\u003e` timeout in ms\n    - `retry \u003cNumber\u003e` attempts to make before giving up. default is 1\n  - `callback \u003cFunction\u003e`\n\nSends a single request to a RPC server/worker.\n[Example](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_client.js).\n\n\n#### peer.stream(name, opts)\n  - `name \u003cString\u003e` Name of the service to address\n  - `options \u003cObject\u003e` Options for the request\n    - `timeout \u003cNumber\u003e` timeout in ms\n    - `headers \u003cObject\u003e` Headers to add to the request\n\nLooks a service up and returns a req-object which is a stream.\nAdditional parameters (e.g. content-type), can be added via options.\n\nThe default metadata values for the request id and key are automatically\nvia header.\n\n[Example](https://github.com/bitfinexcom/grenache-nodejs-http/tree/master/examples/rpc_client_stream.js).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitfinexcom%2Fgrenache-nodejs-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitfinexcom%2Fgrenache-nodejs-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitfinexcom%2Fgrenache-nodejs-http/lists"}