Exploring .NET 10’s New Server-Sent Events Feature
Microsoft’s .NET 10 introduces one of its most exciting features yet:
Server-Sent Events (SSE).
This new capability enables developers to stream events directly from a server to a client — without relying on WebSockets, SignalR, or webhooks.
If you’re building APIs or web applications, this feature could simplify real-time data delivery in your projects.
What Are Server-Sent Events?
Server-Sent Events (SSE) allow a server to push updates to a client over a single HTTP GET request.
The communication is unidirectional — meaning data only flows from server to client.
This makes SSE ideal for scenarios like:
- Stock tickers and live financial data
- Real-time order tracking in e-commerce apps
- Notifications and activity streams
- Live dashboards
Implementing SSE in .NET 10
To demonstrate, let’s start with a simple minimal API in .NET 10.
We’ll create a /live-orders endpoint that streams new order data to clients in real time.
Step 1: Define the Endpoint
1 2 3 4 5 |
app.MapGet("/live-orders", async (HttpContext context, CancellationToken token) => { var orders = GetOrdersAsync(token); return Results.Extensions.ServerSentEvents(orders); }); |
Here, instead of returning a standard JSON response, we return server-sent events.
Step 2: Create a Stream of Data
We’ll use an IAsyncEnumerable to continuously generate and return order objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
async IAsyncEnumerable [EnumeratorCancellation] CancellationToken cancellationToken) { var generator = new OrderGenerator(); while (!cancellationToken.IsCancellationRequested) { await Task.Delay(2000, cancellationToken); yield return new SseItem data: generator.CreateOrder(), eventId: Guid.NewGuid().ToString(), eventType: "order", retry: TimeSpan.FromMinutes(1) ); } } |
Each SseItem contains:
- Data (in this case, a FoodOrder)
- Event ID
- Event type (helps clients filter events)
- Retry interval (used if the connection drops)
Step 3: Consuming SSE in the Browser
Browsers natively support SSE using the EventSource API.
You only need a few lines of JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const eventSource = new EventSource("/live-orders"); eventSource.addEventListener("order", (event) => { const order = JSON.parse(event.data); const list = document.getElementById("orders"); const item = document.createElement("li"); item.textContent = `${order.name} - $${order.price}`; list.appendChild(item); }); eventSource.onerror = (error) => { console.error("Connection error:", error); }; |
This setup automatically reconnects if the connection drops, ensuring reliability.
Why Use SSE Instead of WebSockets or SignalR?
While WebSockets and SignalR support
bi-directional communication, many real-time applications only require
server-to-client streaming.
SSE is:
- Simpler — built into HTTP, no extra libraries needed
- Lightweight — fewer overheads compared to WebSockets
- Browser-native — widely supported without plugins
- Persistent — automatically reconnects if the connection fails
For use cases where you only need one-way communication, SSE is an elegant solution.
Final Thoughts
The new Server-Sent Events feature in .NET 10 opens the door to easier and more efficient real-time applications.
Whether you’re building financial dashboards, live order systems, or notification services, this feature offers a
lightweight alternative to SignalR and WebSockets.
If you’re curious about diving deeper, Microsoft also introduced .NET Aspire,
a framework for managing distributed applications — and it integrates seamlessly with SSE.
Now the question is:
Will you replace some of your WebSocket or SignalR implementations with SSE in .NET 10?
Related Posts
Leave a Reply Cancel reply
Service
Categories
- DEVELOPMENT (104)
- DEVOPS (53)
- FRAMEWORKS (28)
- IT (25)
- QA (14)
- SECURITY (14)
- SOFTWARE (13)
- UI/UX (6)
- Uncategorized (8)