Nethermind.Libp2p.Protocols.RequestResponse 1.0.0-ci.20260930113755

This is a prerelease version of Nethermind.Libp2p.Protocols.RequestResponse.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Nethermind.Libp2p.Protocols.RequestResponse --version 1.0.0-ci.20260930113755
                    
NuGet\Install-Package Nethermind.Libp2p.Protocols.RequestResponse -Version 1.0.0-ci.20260930113755
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nethermind.Libp2p.Protocols.RequestResponse" Version="1.0.0-ci.20260930113755" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nethermind.Libp2p.Protocols.RequestResponse" Version="1.0.0-ci.20260930113755" />
                    
Directory.Packages.props
<PackageReference Include="Nethermind.Libp2p.Protocols.RequestResponse" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nethermind.Libp2p.Protocols.RequestResponse --version 1.0.0-ci.20260930113755
                    
#r "nuget: Nethermind.Libp2p.Protocols.RequestResponse, 1.0.0-ci.20260930113755"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Nethermind.Libp2p.Protocols.RequestResponse@1.0.0-ci.20260930113755
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nethermind.Libp2p.Protocols.RequestResponse&version=1.0.0-ci.20260930113755&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Nethermind.Libp2p.Protocols.RequestResponse&version=1.0.0-ci.20260930113755&prerelease
                    
Install as a Cake Tool

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:

  1. Listen for incoming connections
  2. Deserialize the request message
  3. Call your handler function
  4. Serialize and send the response
  5. 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 responses
    • loggerFactory: Optional logger factory for debugging
    • expectsResponse: Optional predicate for protocols that mix requests and one-way messages. Defaults to expecting a response.
  • Key Methods:

    • ListenAsync(): Handles incoming requests from remote peers
    • DialAsync(): Sends requests to remote peers and waits for responses

When expectsResponse returns false, DialAsync() completes after a successful write and returns an empty TResponse; this is not a remote acknowledgement. ListenAsync() still invokes the handler but does not send its response.

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,
    Func<TRequest, bool>? expectsResponse = null)

Parameters:

  • builder: The peer factory builder to extend
  • protocolId: Unique protocol identifier
  • handler: Function to handle incoming requests
  • isExposed: Whether the protocol should be advertised to other peers
  • expectsResponse: Whether each request expects a wire response

Type Constraints

Both TRequest and TResponse must satisfy:

where TRequest : class, IMessage<TRequest>, new()
where TResponse : class, IMessage<TResponse>, new()
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-ci.20260930141803 0 9/30/2026
1.0.0-ci.20260930113755 0 9/30/2026
1.0.0-ci.20260930110420 0 9/30/2026
1.0.0-ci.20260930104500 0 9/30/2026