-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add support for emitting ServerSentEvents from minimal APIs #56172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
FYI; SSE in firefox has had a long standing bug where EventSource in Javascript doesn't fire the Open event until the server has started sending data. We can also steal (share) the writing side of SSE from SignalR https://source.dot.net/#Microsoft.AspNetCore.Http.Connections/ServerSentEventsMessageFormatter.cs,c4a21cef091f2b21 |
Given that System.Net.ServerSentEvents is only exposed an OOB NuGet package, does it create any challenges when it comes to adding it as a dependency in the aspnetcore shared framework? |
It would end up being pulled into the aspnetcore shared framework. That happens with several other runtime packages. |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
@captainsafia under current API proposal what controls how individual
This is a non-standard extension of the SSE specification, and it can only be achieved using either a custom marshalling delegate as with the System.Net.ServerSentEvents APIs or exposing an overload specifically accepting |
Correct. It defaults to the built-in serializer.
To this end, I consdiered adding an overload that allows end-users to customize the |
Am I correct in assuming that the default handling of Exposing an overload that takes a formatting callback would serve to unblock our use case, however it still puts the onus on the application author to create a callback that formats strings as raw strings. One possibility is specializing handling of |
// Assembly: Microsoft.AspNetCore.Http.Results
namespace Microsoft.AspNetCore.Http;
public static partial class TypedResults
{
public static ServerSentEventsResult<string> ServerSentEvents(
IAsyncEnumerable<string> values,
string? eventType = null);
public static ServerSentEventsResult<T> ServerSentEvents<T>(
IAsyncEnumerable<T> values,
string? eventType = null);
public static ServerSentEventsResult<SseItem<T>> ServerSentEvents<T>(
IAsyncEnumerable<SseItem<T>> values);
}
public static partial class Results
{
public static IResult ServerSentEvents(
IAsyncEnumerable<string> values,
string? eventType = null);
public static IResult ServerSentEvents<T>(
IAsyncEnumerable<T> values,
string? eventType = null);
public static IResult ServerSentEvents<T>(
IAsyncEnumerable<SseItem<T>> values);
}
public sealed class ServerSentEventsResult<T> :
IResult,
IEndpointMetadataProvider,
IStatusCodeHttpResult
{
static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, EndpointBuilder builder);
public Task ExecuteAsync(HttpContext httpContext);
public int? StatusCode { get; }
} |
This enables us to support SSE in minimal APIs for dotnet/aspnetcore#56172.
This does indeed cause ambiguity issues when attempting to determine whether the To that end, the following modification was made to the API in implementation. - public static ServerSentEventsResult<SseItem<T>> ServerSentEvents<T>(
+ public static ServerSentEventsResult<T> ServerSentEvents<T>(
IAsyncEnumerable<SseItem<T>> values); Some other considerations from PR review:
|
Very happy to see this will be included in .NET 10. I am using Datastar (framework like HTMX but with frontend signals and SSE) and it's nice to see examples for long-lived connections that send multiple events. The current Datastar .NET SDK works really well to send SSE events specifically for Datastar. |
Marking this as closed to be released in .NET 10 Preview 3. Docs issue is over at dotnet/AspNetCore.Docs#35089. Sample app is over at https://github.com/captainsafia/minapi-sse. |
Background and Motivation
This proposal adds first-class support for SSE responses through the
IResult
pattern, making it consistent with other response types in minimal APIs. While ASP.NET Core has supported SSE through manual response writing, there hasn't been a built-in IResult implementation to return SSE streams from minimal API endpoints.Proposed API
Usage Examples
Basic usage with simple values:
Usage with custom event types:
Raw string processing with
string
overload:Fine-grained control with the
SseItem<T>
overload:Alternative Designs
TypedResults
extension methods do not expose an overload that takes an event ID. Instead, the user must use theSseItem<T>
based overload of the extension method if they want to control the event ID at a more granular level.Risks
N/A
The text was updated successfully, but these errors were encountered: