Skip to content

Request-Response

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.

The Request-Response service

from hyveos_sdk import Connection
import asyncio
async with Connection() as connection:
req_resp = connection.get_request_response_service()
# continue with usage of req_resp
# TODO

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.

requesting_node.py
from hyveos_sdk import Connection
import asyncio
async with Connection() as connection:
req_resp = connection.get_request_response_service()
peer_id = "0123456789ABCDEF"
topic = "robot_status"
message_data = "give_status"
response = await req_resp.send_request(peer_id, message_data, topic)
response_data = loads(response.data.data)
print(f"Received response: {response_data}")
asyncio.run(main())

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

  1. Receive the message through an incoming stream at the remote node
  2. Process the message
  3. 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.

receiving_node.py
from hyveos_sdk import Connection
import asyncio
async def main():
async with Connection() as connection:
req_resp = connection.get_request_response_service()
topic = "robot_status"
async with req_resp.receive(topic) as requests:
# Handle stream of incoming requests -> RecvRequest
# TODO
pass
asyncio.run(main())

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.

receiving_node.py
# continuing Example 3
async for request in requests:
print(f"Request from {request.peer.peer_id}")
seq = request.seq
received = request.msg
print(f"Data: {received}")
# respond with seq
# TODO

Example 4. Retrieving fields from the request. In Rust and Typescript, this means leveraging InboundRequest.

Responding to a request

receiving_node.py
# Continuing from Example 4
if received is "give_status":
await req_resp.respond(seq, "All systems operational.")
else:
await req_resp.respond(seq, None, "Unknown request") # Error

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.


© 2025 P2P Industries. This documentation is licensed under the MIT License.
Cookie Policy    Privacy Policy