### Install and Start Grenache Grape DHT Nodes Source: https://github.com/bitfinexcom/grenache-nodejs-http/blob/master/README.md This section outlines the installation of the `grenache-grape` package globally and provides commands to start two Grape DHT nodes. These nodes are configured with specific distributed and API HTTP ports, and are bootstrapped to each other to form a network. ```bash npm i -g grenache-grape ``` ```bash grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002' grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001' ``` -------------------------------- ### Grenache RPC Server and Client Example Source: https://github.com/bitfinexcom/grenache-nodejs-http/blob/master/README.md This comprehensive example illustrates a full RPC communication flow using Grenache. It includes the necessary Grape setup, a server that announces a service and handles incoming requests, and a client that sends a request to the announced service and processes the response. The communication leverages the underlying DHT for peer discovery. ```bash grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002' grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001' ``` ```js const Link = require('grenache-nodejs-link') const link = new Link({ grape: 'http://127.0.0.1:30001' }) link.start() const peer = new PeerRPCServer(link, { timeout: 300000 }) peer.init() const service = peer.transport('server') service.listen(_.random(1000) + 1024) setInterval(function () { link.announce('rpc_test', service.port, {}) }, 1000) service.on('request', (rid, key, payload, handler) => { console.log(payload) // hello handler.reply(null, 'world') }) ``` ```js const Link = require('grenache-nodejs-link') const link = new Link({ grape: 'http://127.0.0.1:30001' }) link.start() const peer = new PeerRPCClient(link, {}) peer.init() peer.request('rpc_test', 'hello', { timeout: 10000 }, (err, data) => { if (err) { console.error(err) process.exit(-1) } console.log(data) // world }) ``` -------------------------------- ### Implement a Grenache RPC Server Source: https://github.com/bitfinexcom/grenache-nodejs-http/blob/master/README.md This JavaScript example demonstrates how to set up an RPC server using `grenache-nodejs-link` and `PeerRPCServer`. It announces a service named `rpc_test` on the overlay network, listens for incoming requests, and replies with 'world' when receiving 'hello' from a client. ```js const Link = require('grenache-nodejs-link') const link = new Link({ grape: 'http://127.0.0.1:30001' }) link.start() const peer = new PeerRPCServer(link, {}) peer.init() const service = peer.transport('server') service.listen(_.random(1000) + 1024) setInterval(function () { link.announce('rpc_test', service.port, {}) }, 1000) service.on('request', (rid, key, payload, handler) => { console.log(payload) // hello handler.reply(null, 'world') }) ``` -------------------------------- ### Install Grenache Node.JS HTTP Package Source: https://github.com/bitfinexcom/grenache-nodejs-http/blob/master/README.md This snippet demonstrates how to install the `grenache-nodejs-http` package using npm, saving it as a dependency in your project. ```bash npm install --save grenache-nodejs-http ``` -------------------------------- ### Implement a Grenache RPC Client Source: https://github.com/bitfinexcom/grenache-nodejs-http/blob/master/README.md This JavaScript example shows how to create an RPC client using `grenache-nodejs-link` and `PeerRPCClient`. It sends a 'hello' request to the `rpc_test` service announced by the server and logs the 'world' response received, demonstrating basic RPC communication. ```js const Link = require('grenache-nodejs-link') const link = new Link({ grape: 'http://127.0.0.1:30001' }) link.start() const peer = new PeerRPCClient(link, {}) peer.init() peer.request('rpc_test', 'hello', { timeout: 10000 }, (err, data) => { if (err) { console.error(err) process.exit(-1) } console.log(data) // world }) ``` -------------------------------- ### API: PeerRPCServer Class Documentation Source: https://github.com/bitfinexcom/grenache-nodejs-http/blob/master/README.md Detailed API documentation for the `PeerRPCServer` class, outlining its constructor, methods, properties, and events. It covers how to initialize a server, set it to listen for connections, and handle various types of incoming requests, including streaming data. ```APIDOC Class: PeerRPCServer Event: 'stream' Description: Always emitted as soon as a request arrives. Emits the raw `req` and `res` streams of the request and some preparsed metadata. Used for streaming. If `disableBuffered` is set to `false`, the server will attempt to buffer after emitting the stream event. Code Example: serviceStr.on('stream', (req, res, meta, handler) => { console.log(meta) // meta.isStream === true const [rid, key] = meta.infoHeaders req.pipe(process.stdout) handler.reply(rid, null, 'world') // convenience reply }) Event: 'request' Description: Emitted when a request from a RPC client is received. In the lifecycle of a request this happens after the server has parsed an buffered the whole data. When the server runs with `disableBuffered: true`, the event must emitted manually, if needed, or by calling the buffering request handlers manually. Parameters: - rid: unique request id - key: name of the service - payload: Payload sent by client - handler: Handler object, used to reply to a client. Code Example: service.on('request', (rid, key, payload, handler) => { handler.reply(null, 'world') }) new PeerRPCServer(link, [options]) Description: Creates a new instance of a `PeerRPCServer`, which connects to the DHT using the passed `link`. Parameters: - link : Instance of a [Link Class](#new-linkoptions) - options : - disableBuffered : Disable automatic buffering of the incoming request data stream. Useful for streaming. - timeout : Server-side socket timeout - secure : TLS options - key - cert - ca - requestCert - rejectUnauthorized peer.init() Description: Sets the peer active. Must get called before we get a transport to set up a server. peer.transport('server') Description: Must get called after the peer is active. Sets peer into server-mode. peer.listen(port) Description: Lets the `PeerRPCServer` listen on the desired `port`. The port is stored in the DHT. peer.port Description: Port of the server (set by `listen(port)`). ``` -------------------------------- ### PeerRPCClient Class API Reference Source: https://github.com/bitfinexcom/grenache-nodejs-http/blob/master/README.md Detailed API documentation for the `PeerRPCClient` class, including its constructor and methods for making RPC requests and managing connections within the Grenache network. This section outlines parameters, return types, and usage constraints for each method. ```APIDOC Class: PeerRPCClient new PeerRPCClient(link, [options]) link Instance of a [Link Class](#new-linkoptions) options maxActiveKeyDests maxActiveDestTransports secure TLS options key cert ca rejectUnauthorized Description: Creates a new instance of a "PeerRPCClient", which connects to the DHT using the passed "link". A PeerRPCClient can communicate with multiple Servers and map work items over them. With "maxActiveKeyDests" you can limit the maximum amount of destinations. Additionally, you can limit the amount of transports with "maxActiveDestTransports". peer.init() Description: Sets the peer active. Must get called before we start to make requests. peer.map(name, payload, [options], callback) name Name of the service to address payload Payload to send options Options for the request timeout timeout in ms limit maximum requests per available worker callback Description: Maps a number of requests over the amount of registered workers / PeerRPCServers. peer.request(name, payload, [options], callback) name Name of the service to address payload Payload to send options Options for the request timeout timeout in ms retry attempts to make before giving up. default is 1 callback Description: Sends a single request to a RPC server/worker. peer.stream(name, opts) name Name of the service to address options Options for the request timeout timeout in ms headers Headers to add to the request Description: Looks a service up and returns a req-object which is a stream. Additional parameters (e.g. content-type), can be added via options. The default metadata values for the request id and key are automatically via header. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.