Nethermind.Libp2p.Protocols.RequestResponse
1.0.0-preview.51
dotnet add package Nethermind.Libp2p.Protocols.RequestResponse --version 1.0.0-preview.51
NuGet\Install-Package Nethermind.Libp2p.Protocols.RequestResponse -Version 1.0.0-preview.51
<PackageReference Include="Nethermind.Libp2p.Protocols.RequestResponse" Version="1.0.0-preview.51" />
<PackageVersion Include="Nethermind.Libp2p.Protocols.RequestResponse" Version="1.0.0-preview.51" />
<PackageReference Include="Nethermind.Libp2p.Protocols.RequestResponse" />
paket add Nethermind.Libp2p.Protocols.RequestResponse --version 1.0.0-preview.51
#r "nuget: Nethermind.Libp2p.Protocols.RequestResponse, 1.0.0-preview.51"
#:package Nethermind.Libp2p.Protocols.RequestResponse@1.0.0-preview.51
#addin nuget:?package=Nethermind.Libp2p.Protocols.RequestResponse&version=1.0.0-preview.51&prerelease
#tool nuget:?package=Nethermind.Libp2p.Protocols.RequestResponse&version=1.0.0-preview.51&prerelease
RequestResponse Protocol
The RequestResponse Protocol provides a generic, type-safe implementation of request-response pattern in libp2p. It enables peers to exchange structured data using Protocol Buffers serialization with automatic message parsing and error handling.
Overview
The RequestResponseProtocol<TRequest, TResponse> class implements the ISessionProtocol<TRequest, TResponse> interface, providing:
- Type Safety: Strongly typed request and response messages
- Protocol Buffers: Automatic serialization/deserialization using Google.Protobuf
- Error Handling: Comprehensive logging and exception management
- Bidirectional Communication: Support for both client (dial) and server (listen) operations
Usage
Register the Protocol
Use the extension method to register your protocol with the peer factory:
using Nethermind.Libp2p.Protocols;
// Register the protocol with a handler function
var peerFactory = new PeerFactoryBuilder()
.AddRequestResponseProtocol<ExampleRequest, ExampleResponse>(
protocolId: "/example/1.0.0",
handler: HandleExampleRequest,
loggerFactory: loggerFactory,
isExposed: true)
.Build();
// Handler function implementation
private static async Task<ExampleResponse> HandleExampleRequest(
ExampleRequest request,
ISessionContext context)
{
// Process the request
var results = ProcessQuery(request.Query, request.Limit);
return new ExampleResponse
{
Results = { results },
Success = true
};
}
Using the Protocol
As a Client (Dialing)
// Connect to a remote peer
ISession session = await localPeer.DialAsync(remoteAddress);
// Send a request and receive a response
var request = new ExampleRequest
{
Query = "search term",
Limit = 10
};
var response = await session.DialAsync<RequestResponseProtocol<ExampleRequest, ExampleResponse>,
ExampleRequest,
ExampleResponse>(request);
Console.WriteLine($"Success: {response.Success}");
Console.WriteLine($"Results: {string.Join(", ", response.Results)}");
As a Server (Listening)
The server-side handling is automatic when you register the protocol with a handler function. The protocol will:
- Listen for incoming connections
- Deserialize the request message
- Call your handler function
- Serialize and send the response
- Close the connection
Protocol Implementation Details
RequestResponseProtocol<TRequest, TResponse>
The core protocol class provides:
Constructor Parameters:
protocolId: Unique identifier for the protocol (e.g., "/my-app/1.0.0")handler: Async function to process requests and generate responsesloggerFactory: Optional logger factory for debugging
Key Methods:
ListenAsync(): Handles incoming requests from remote peersDialAsync(): Sends requests to remote peers and waits for responses
RequestResponseExtensions
The extension class provides a convenient builder method:
public static IPeerFactoryBuilder AddRequestResponseProtocol<TRequest, TResponse>(
this IPeerFactoryBuilder builder,
string protocolId,
Func<TRequest, ISessionContext, Task<TResponse>> handler,
bool isExposed = true)
Parameters:
builder: The peer factory builder to extendprotocolId: Unique protocol identifierhandler: Function to handle incoming requestsisExposed: Whether the protocol should be advertised to other peers
Type Constraints
Both TRequest and TResponse must satisfy:
where TRequest : class, IMessage<TRequest>, new()
where TResponse : class, IMessage<TResponse>, new()
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Google.Protobuf (>= 3.31.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Nethermind.Libp2p.Core (>= 1.0.0-preview.51)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Nethermind.Libp2p.Protocols.RequestResponse:
| Package | Downloads |
|---|---|
|
Nethermind.Libp2p
A libp2p implementation for .NET |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-preview.51 | 1,430 | 11/11/2025 |
| 1.0.0-preview.50 | 124 | 11/7/2025 |