A full-stack demonstration project showcasing Server-Sent Events (SSE) for real-time server to client communication between an ASP.NET Core backend and an Angular frontend.
This project illustrates how to implement efficient real-time data streaming using Server-Sent Events, with a focus on:
- Streaming data chunks immediately as they become available
- Proper HTTP connection lifecycle management
- Client-side consumption of SSE events
- Graceful cancellation handling
Streaming-Sample/
βββ backend/ # ASP.NET Core API with SSE endpoint
βββ frontend/ # Angular application consuming SSE events
βββ README.md # This file
- .NET SDK (for backend)
- Node.js (for frontend)
- A modern web browser
cd backend
dotnet restore
dotnet runThe API will start on http://localhost:5000 (or the configured port).
cd frontend
npm install
npm startThe application will open in your browser at http://localhost:4200.
Location: backend/
- Endpoint:
GET /api/streaming/progress - Content-Type:
text/event-stream; charset=utf-8 - Pattern: Long-lived HTTP connection with streaming responses
StreamingControllerexposes the SSE endpointStreamingSdkClientsimulates data production asIAsyncEnumerable<string>- Server flushes each chunk immediately for real-time delivery
- Supports automatic cancellation when client disconnects
data: "Chunk 1 at 2026-05-19T..."
data: "Chunk 2 at 2026-05-19T..."
Each message is prefixed with data: and terminated by a blank line.
- Represents an async stream of values produced over time
- Yields each SDK chunk immediately instead of buffering
- Naturally supports cancellation via
CancellationToken - Controller consumes with
await foreachand sends each event
For detailed information, see backend/README.md.
Location: frontend/
streaming.service.tscreates anEventSourceconnection to the backendstreaming.component.tssubscribes to the service and displays messages in real-time
- Automatic reconnection on connection loss
- Real-time message rendering
- Observable-based architecture for easy integration with Angular
Update the API URL in src/app/streaming.component.ts if your backend is hosted elsewhere:
private apiUrl = 'http://localhost:5000/api/streaming/progress';For detailed information, see frontend/README.md.
βββββββββββββββββββββββββββ HTTP GET ββββββββββββββββββββββββ
β Angular Frontend ββββββββββββββββββββββββββββΆβ ASP.NET Core API β
β β β β
β - EventSource listener ββββββββββ SSE Stream βββββββ - Streaming endpoint β
β - Real-time display β (text/event-stream) β - IAsyncEnumerable β
β β β - Immediate flush β
βββββββββββββββββββββββββββ ββββββββββββββββββββββββ
You can test the backend endpoint using curl:
curl -N http://localhost:5000/api/streaming/progressThe -N flag disables buffering to see real-time events.
- Port: Configured in
launchSettings.json(default:5000) - Chunk interval: Configurable in
StreamingSdkClient
- API URL: Update in
streaming.component.ts - Port: Configured in
angular.json(default:4200)
- The backend uses controller-based APIs, not minimal APIs
- Ensure CORS is properly configured if running on different domains
- The frontend must connect using
EventSourceand listen foronmessageevents - Both projects include development configurations for easy local testing
If frontend and backend are on different origins, configure CORS in the backend's Startup.cs or Program.cs:
services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});This project is provided as-is for educational and demonstration purposes.
Feel free to fork, modify, and adapt this sample for your own projects!
Happy streaming! π