In modern web development, the need to update the user interface in real time is now standard. Historically, the simplest (but least efficient) approach has been HTTP Polling, in which the client bombards the server with requests at regular intervals.
This approach involves an enormous waste of resources, latency, and unnecessary load on the server.
While for complex bidirectional communication the ideal choice falls on SignalR (WebSockets), there's a perfect middle ground for unidirectional data flows (from server to client): Server-Sent Events (SSE). With the release of .NET 10, ASP.NET Core finally introduces first-class native support that makes implementing SSE incredibly simple and clean.
Why choose SSE over Polling (and SignalR)?
Server-Sent Events rely on a single standard HTTP connection that stays open. The server pushes data to the client as soon as it's available, formatted according to the text/event-stream type.
- No Polling: The client makes a single HTTP request. No infinite loop of requests every $X$ seconds.
- Standard Infrastructure: Works over HTTP/1.1 or HTTP/2, with no need for protocol upgrades (as required by WebSockets). Works natively with proxies, firewalls, and load balancers.
- Automatic Reconnection: If the network drops, the browser automatically attempts to reconnect via the native
EventSourceAPI, sending the last received ID (Last-Event-ID) to avoid data loss. - Lightweight: Compared to SignalR, it doesn't require heavy client libraries or complex server-side configuration.
The .NET 10 Breakthrough: Results.ServerSentEvents
In previous .NET versions, implementing SSE required manually configuring HttpContext.Response headers, writing formatted strings into the stream body, and manually handling buffer flushing and cancellation tokens.
In .NET 10, all of this is abstracted away thanks to the new Results.ServerSentEvents method (or TypedResults.ServerSentEvents) and the native SseItem<T> struct included in the System.Net.ServerSentEvents namespace. The framework autonomously handles JSON serialization, stream formatting, and respecting client cancellation.
Practical Example: Streaming Updates with .NET 10
Below is a complete example. The backend simulates a continuous stream of weather data sent to the client every 2 seconds without interruption.
1. The Backend (.NET 10 Minimal API)
Make sure you're using the .NET 10 SDK. Create an endpoint that returns an IAsyncEnumerable integrated with SseItem<T>.
C#
2. The Frontend (Vanilla JavaScript)
On the client side no external library is needed. We leverage the native EventSource object provided by all modern browsers.
HTML
Conclusions
With .NET 10, Server-Sent Events step out of the shadow of custom implementations to become a first-class citizen in ASP.NET Core.
If your application only needs to receive updated data from the server (as with dashboards, notification feeds, streaming logs, or progressive responses from Artificial Intelligence/LLM models), SSE with TypedResults.ServerSentEvents is the architecturally cleanest, lightest, and most efficient solution at your disposal, finally putting the old, heavy polling mechanism out to pasture.
Comments (0)
No comments yet.