dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.1k stars 9.91k forks source link

Add support for emitting ServerSentEvents from minimal APIs #56172

Open captainsafia opened 2 months ago

captainsafia commented 2 months ago

As of https://github.com/dotnet/runtime/pull/102238 and https://github.com/dotnet/runtime/issues/98105, .NET exposes a set of types for working with server-sent events. We want to extend on these APIs in minimal APIs by provide IResult implementations for emitting SSEs to the response.

BrennanConroy commented 2 months ago

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. See https://source.dot.net/#Microsoft.AspNetCore.Http.Connections/Internal/Transports/ServerSentEventsServerTransport.cs,45 for what we do in SignalR. It might not be relevant to Minimal since when we return the SSE IResult we'll be writing SSE frames at that point? Unless we want bi-directional SSE in which case we'd somehow need to send a comment frame when app code wants to read SSE data.

We can also steal (share) the writing side of SSE from SignalR https://source.dot.net/#Microsoft.AspNetCore.Http.Connections/ServerSentEventsMessageFormatter.cs,c4a21cef091f2b21