The request-response service enables a direct, unicast exchange of data between two nodes.
Unlike pub-sub, which broadcasts to many subscribers, request-response expects a direct response for every request sent.
This pattern is especially useful when a node wants to request another node’s ability
to handle a specific task e.g., checking battery levels, resource availability, or configuration setting.
Example 1. Obtain the Request-Response service handler. Note the async environment.
Sending Requests
To query a specific node for data or ask it to perform an action, send a request. Typically, it will hold the target peer’s ID and an optional topic
to which that peer is registered. Topics can and should mainly be used to logically group requests.
The receiving peer must be subscribed to the topic specified in the request in order to receive the request.
Example 2. Simple request of status information through request/response without error handling.
You might want to include try/catch in actual production code.
Receiving & Responding to Requests
The remote node will have to react to the sent request.
Workflow
Receive the message through an incoming stream at the remote node
Process the message
Respond, with a handler (currently in Rust and Typescript, but not Python yet)
Receive a Request
Messages will be received through a stream of inbound receive messages.
Example 3. Receiving the request of giving the status information.
Example 4 below will explain the processing of inbound requests.
Process Inbound Request
In the Pyhton SDK, you explicitly receive a seq number upon receive().
seq is then given in respond() for matching the request to the eventual response.
The Rust and Typescript SDKs have a InboundRequestHandle object for takes care of this.
Each inbound message in Rust comes in a (request, handle) pair.
request is of type InboundRequest, handle is of type InboundRequestHandle.
Retrieve the the fields of request when handling the inbound request.
Example 5. Use the InboundRequestHandle for responding. In Rust and Typescript, you can respond with an error as well.
Note that in Python in case of an error, you can leave the data argument to None and give an error message in the
last argument—here: "Unknown request".
Pyhton responding incorporates explicitly giving the seq number in the argument of respond().
JSON and CBOR Request/Response Services
While raw bytes give you flexibility, you’ll often want structured data.
hyveOS offers typed request/response services for JSON and CBOR.
The same code examples apply analogously.
All methods send_request(), recv() and respond() are in called the same way for
these services. The JSON and CBOR services are just convenient data format wrapper services.