Lewee.Infrastructure.ServerEvents 0.40.3

dotnet add package Lewee.Infrastructure.ServerEvents --version 0.40.3
                    
NuGet\Install-Package Lewee.Infrastructure.ServerEvents -Version 0.40.3
                    
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="Lewee.Infrastructure.ServerEvents" Version="0.40.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lewee.Infrastructure.ServerEvents" Version="0.40.3" />
                    
Directory.Packages.props
<PackageReference Include="Lewee.Infrastructure.ServerEvents" />
                    
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 Lewee.Infrastructure.ServerEvents --version 0.40.3
                    
#r "nuget: Lewee.Infrastructure.ServerEvents, 0.40.3"
                    
#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 Lewee.Infrastructure.ServerEvents@0.40.3
                    
#: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=Lewee.Infrastructure.ServerEvents&version=0.40.3
                    
Install as a Cake Addin
#tool nuget:?package=Lewee.Infrastructure.ServerEvents&version=0.40.3
                    
Install as a Cake Tool

Lewee.Infrastructure.ServerEvents

Infrastructure utilities for ASP.NET Core Web API Server-Sent Events.

Purpose

This package provides the server-side infrastructure for broadcasting client events to connected Blazor clients using Server-Sent Events (SSE). It includes the client event broadcaster, channel configuration, and SSE endpoint.

For client-side event reception in Blazor applications, see Lewee.Infrastructure.Fluxor.

Dependencies

  • Microsoft.AspNetCore.App - ASP.NET Core framework
  • Lewee.Application - Application layer with ClientEvent notification
  • Lewee.Common - Common utilities

Components

ClientEventChannelHandler

MediatR notification handler that fans out ClientEvent notifications to all active SSE connections (per user, or broadcast when UserId is null/empty).

internal sealed class ClientEventChannelHandler : INotificationHandler<ClientEvent>
{
    public async Task Handle(ClientEvent notification, CancellationToken cancellationToken)
    {
        // Writes the event to all per-connection channels for the user (or all channels when broadcasting)
    }
}

SSE Endpoint Configuration

AddClientEventBroadcaster

Configures the client event channel and handler:

builder.Services.AddClientEventBroadcaster();

This method:

  • Registers ConnectionManager as a singleton to manage per-connection channels
  • Registers ClientEventChannelHandler as a notification handler for ClientEvent fan-out
MapSseEndpoint

Maps the SSE endpoint at /events for client connections:

app.MapSseEndpoint();

The endpoint:

  • Requires authorization
  • Filters events by the authenticated user's ID
  • Streams events as Server-Sent Events

Usage

Server Setup

// In Web API Program.cs
builder.Services.AddClientEventBroadcaster();

var app = builder.Build();
app.MapSseEndpoint();

Publishing Events

// In command handler or any service
public class CreateOrderHandler : IRequestHandler<CreateOrderCommand, CommandResult>
{
    private readonly IMediator mediator;

    public async Task<CommandResult> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
    {
        // ... create order logic ...

        // Publish event to channel for broadcasting to connected clients
        await mediator.Publish(new ClientEvent
        {
            ContractFullClassName = "OrderCreated",
            UserId = order.UserId,
            Data = JsonSerializer.Serialize(orderDto)
        }, cancellationToken);

        return CommandResult.Success();
    }
}

For receiving events in Blazor, see Lewee.Infrastructure.Fluxor.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
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
0.40.3 49 11/22/2026