MVFC.LongPolling 1.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package MVFC.LongPolling --version 1.0.7
                    
NuGet\Install-Package MVFC.LongPolling -Version 1.0.7
                    
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="MVFC.LongPolling" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.LongPolling" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.LongPolling" />
                    
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 MVFC.LongPolling --version 1.0.7
                    
#r "nuget: MVFC.LongPolling, 1.0.7"
                    
#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 MVFC.LongPolling@1.0.7
                    
#: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=MVFC.LongPolling&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.LongPolling&version=1.0.7
                    
Install as a Cake Tool

MVFC.LongPolling

πŸ‡§πŸ‡· Leia em PortuguΓͺs

CI codecov License Platform NuGet Version NuGet Downloads

A lightweight and efficient long polling via Redis Pub/Sub library for .NET, with configurable CancellationToken support and typed payload.

Objective

Allow HTTP clients to await asynchronous results (jobs, webhooks, processing) in a simple way, without blind polling or WebSockets, using the Redis Pub/Sub channel as the notification mechanism.


Features

  • Redis Pub/Sub: Real subscription per channel, no key polling.
  • Configurable timeout: Maximum wait time globally or per call via LongPollingOptions.
  • CancellationToken: Automatically respects HTTP client disconnection.
  • Typed payload: WaitAsync<T> deserializes the result directly via System.Text.Json.
  • Confirmed delivery: PublishAsync returns false if no subscriber was active.
  • Deterministic synchronization: WaitUntilReadyAsync guarantees the subscription is active before publishing.
  • Fluent configuration: Simple setup in Program.cs.
  • Channel prefix: Environment isolation via KeyPrefix.

Packages

Package Downloads
MVFC.LongPolling Downloads

Installation

dotnet add package MVFC.LongPolling

Configuration

Basic

builder.Services.AddLongPolling("localhost:6379", cfg =>
{
    cfg.DefaultTimeout = TimeSpan.FromSeconds(30);
    cfg.KeyPrefix = "poll";
});

With existing IConnectionMultiplexer

builder.Services.AddLongPolling(existingMultiplexer, cfg =>
{
    cfg.DefaultTimeout = TimeSpan.FromSeconds(20);
});

Usage

Await result (string)

app.MapGet("/poll/{jobId}", async (
    string jobId,
    ILongPollingService polling,
    CancellationToken ct) =>
{
    var result = await polling.WaitAsync(jobId, cancellationToken: ct);

    return result is null
        ? Results.NoContent()
        : Results.Ok(result);
});

Await typed result

app.MapGet("/poll/{jobId}/typed", async (
    string jobId,
    ILongPollingService polling,
    CancellationToken ct) =>
{
    var result = await polling.WaitAsync<OrderCompletedEvent>(jobId, cancellationToken: ct);

    return result is null
        ? Results.NoContent()
        : Results.Ok(result);
});

Notify job completion

app.MapPost("/notify/{jobId}", async (
    string jobId,
    NotifyRequest req,
    ILongPollingService polling) =>
{
    var delivered = await polling.PublishAsync(jobId, req.Payload);

    return delivered
        ? Results.Accepted()
        : Results.NotFound($"No active subscriber for channel '{jobId}'.");
});

Per-call options

var options = new LongPollingOptions(
    Timeout: TimeSpan.FromSeconds(10),
    KeyPrefix: "custom");

var result = await polling.WaitAsync(jobId, options, cancellationToken: ct);

Configuration Parameters

Parameter Type Default Description
DefaultTimeout TimeSpan 30 seconds Maximum wait time per message
KeyPrefix string longpolling Redis channel prefix

How It Works

HTTP Client           Server                       Redis
───────────           ──────                       ─────
GET /poll/{id} ──►   WaitAsync()
                      SubscribeAsync(channel) ──►  SUBSCRIBE poll:id
                      WaitUntilReadyAsync()   ◄──  (confirmed)
                                              ◄──  Worker: PublishAsync()
                      message received        ◄──  PUBLISH poll:id payload
GET returns    ◄──   Results.Ok(payload)

Production Flow Example β€” Payment Scenario

Default flow β€” message received

HTTP Client        Orders API             Payments API
───────────        ──────────             ────────────
POST /orders ──►  Creates order
                   WaitAsync(orderId) ──► Processes payment
                                          PublishAsync(orderId, "approved")
                   ◄── "approved"
200 OK       ◄──  Results.Ok(status)

Timeout β€” no payment response

HTTP Client        Orders API             Payments API
───────────        ──────────             ────────────
POST /orders ──►  Creates order
                   WaitAsync(orderId) ──► Processes payment
                   (waiting...)           (no PublishAsync)
                   timeout reached
504 GW Timeout ◄── Results.StatusCode(504)

Payment declined

HTTP Client        Orders API             Payments API
───────────        ──────────             ────────────
POST /orders ──►  Creates order
                   WaitAsync(orderId) ──► Processes payment
                                          PublishAsync(orderId, "rejected")
                   ◄── "rejected"
422 Unprocessable ◄── Results.UnprocessableEntity(status)

Client disconnects before response

HTTP Client        Orders API             Payments API
───────────        ──────────             ────────────
POST /orders ──►  Creates order
                   WaitAsync(orderId) ──► Processes payment
βœ— disconnects
                   CancellationToken cancelled
                   WaitAsync throws OperationCanceledException
                   (response discarded)

Publish with no active subscriber

HTTP Client        Orders API             Payments API
───────────        ──────────             ────────────
                                          PublishAsync(orderId, "approved")
                                          delivered = false
                   (no active WaitAsync for orderId)
404 Not Found  ◄── Results.NotFound(...)

Project Structure

  • src: Source code for the MVFC.LongPolling library.
  • playground: Sample API to validate behavior with Aspire.
  • tests: Integration tests with Aspire + Redis.

Requirements

.NET 9.0+

Contributing

See CONTRIBUTING.md.

License

Apache-2.0

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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.3 116 11/1/2026
1.1.1 209 6/26/2026
1.1.0 148 4/3/2026
1.0.9 133 3/21/2026
1.0.7 134 3/15/2026
1.0.6 130 3/14/2026
1.0.4 135 2/22/2026
1.0.3 133 2/22/2026
1.0.2 132 2/22/2026
1.0.1 135 2/22/2026