Building real-time apps in .NET?
Chat and notifications are easy — but what about continuous streaming data like:
- Live crypto & Forex prices
- Stock tick feeds
- IoT sensor streams
- AI live token output
- Monitoring dashboards
This is where SignalR Streaming becomes a superpower.
Let’s build it — step-by-step
What Is Streaming in SignalR?
Traditional SignalR sends messages one-by-one.
But some applications need constant data flow.
| Feature | Normal SignalR | SignalR Streaming |
|---|---|---|
| Data pace | On-demand | Continuous |
| Frequency | Low/medium | High (sub-sec data) |
| Use case | Chat, Alerts | Trading feeds, IoT, AI |
| Performance | Good | ⭐ Excellent |
Project Goal
We will build a mini streaming server that sends random market prices every second, and a client that reads them live.
Perfect for learning + a base for real trading dashboards.
🛠 Backend — .NET SignalR Streaming Server
Create a project
dotnet new web -n SignalRStreamingDemo
cd SignalRStreamingDemo
Add SignalR
dotnet add package Microsoft.AspNetCore.SignalR
Create Hub
MarketHub.cs
using Microsoft.AspNetCore.SignalR;
using System.Runtime.CompilerServices;
public class MarketHub : Hub
{
public async IAsyncEnumerable<decimal> StreamPrices(
[EnumeratorCancellation] CancellationToken ct)
{
var rand = new Random();
decimal price = 1000;
while (!ct.IsCancellationRequested)
{
price += (decimal)(rand.NextDouble() - 0.5) * 5;
yield return Math.Round(price, 2);
await Task.Delay(1000, ct);
}
}
}
Wire Hub in Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
var app = builder.Build();
app.MapHub<MarketHub>("/marketHub");
app.Run();
Server
Client — Vanilla JS Receiver
Create:
wwwroot/index.html
<!DOCTYPE html>
<html>
<head>
<title>SignalR Stream Demo</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.5/signalr.min.js"></script>
</head>
<body>
<h3>📈 Live Prices</h3>
<pre id="log"></pre>
<script>
const conn = new signalR.HubConnectionBuilder()
.withUrl("/marketHub")
.build();
async function start() {
await conn.start();
const stream = conn.stream("StreamPrices");
stream.subscribe({
next: p => document.getElementById("log").textContent += p + "\n",
complete: () => console.log("done"),
error: err => console.error(err)
});
}
start();
</script>
</body>
</html>
Run server:
dotnet run
Open browser:
http://localhost:5000
Boom
Live data — every second
Real-World Notes
| Tip | Reason |
|---|---|
Use IAsyncEnumerable<T>
|
Best streaming model |
Include CancellationToken
|
Prevent leaks |
| Throttle updates | Protect UI |
| Use groups per market | Multi-symbol feeds |
| Benchmark before production | Trading needs speed ⚡ |
What You Can Build Next
- Live crypto dashboard
- AI token streaming like ChatGPT
- Forex trading panel
- IoT data monitor
- Game telemetry dashboard
Top comments (0)