Flazlo.Sse 1.0.1

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

SSE Blazor Utility for .NET 8

This library provides streamlined utilities for working with Server-Sent Events (SSE) in both ASP.NET Core backends and Blazor WebAssembly frontends. It enables structured JSON-based communication over text/event-stream, with minimal boilerplate.

Quick Start

Define a structured SSE endpoint:

[HttpGet("/stream")]
public async Task StreamStructured(CancellationToken cancellationToken)
{
    async IAsyncEnumerable<SseMessage> GetStream([EnumeratorCancellation] CancellationToken token)
    {
        int counter = 0;
        while (!token.IsCancellationRequested)
        {
            yield return new SseMessage
            {
                Data = JsonSerializer.Serialize(new { Name = "Mike", Age = counter++ }),
                Event = "heartbeat",
                Id = counter.ToString()
            };

            await Task.Delay(1000, token);
        }
    }

    await HttpContext.StreamAsync(GetStream(cancellationToken), eventName: "heartbeat");
}

Consuming SSE Events (Blazor WebAssembly)

@using SseBlazor
@using System.Text.Json
@inject HttpClient Http

<PageTitle>Counter</PageTitle>

@foreach (var item in messages)
{
    <b>@((MarkupString)string.Format("Name: {0} Age: {1} <br/> EventType: {2} Id: {3}", item.Name, item.Age, item.SSE.Event, item.SSE.Id))</b>
    <br />
}

@code {
    private List<Person> messages = [];
    private SseClient<SseMessage>? _sse;
    private CancellationTokenSource _cts = new();

    protected override async Task OnInitializedAsync()
    {
        _sse = new SseClient<SseMessage>(Http, "/stream");

        await foreach (var evt in _sse.StreamAsync(_cts.Token))
        {
            var message = JsonSerializer.Deserialize<Person>(evt.Data);
            message.SSE = evt;
            messages.Add(message);

            await InvokeAsync(StateHasChanged);
        }
    }

    public void Dispose()
    {
        _cts.Cancel();
        _cts.Dispose();
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public SseMessage SSE { get; set; }
    }
}

Product 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. 
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.0.1 215 10/6/2025
1.0.0 201 8/19/2025