DEV Community

Cover image for Real-Time Streaming in .NET SignalR — Live Data Like Trading Apps
Morteza Jangjoo
Morteza Jangjoo

Posted on

Real-Time Streaming in .NET SignalR — Live Data Like Trading Apps

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
Enter fullscreen mode Exit fullscreen mode

Add SignalR

dotnet add package Microsoft.AspNetCore.SignalR
Enter fullscreen mode Exit fullscreen mode

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Wire Hub in Program.cs

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
var app = builder.Build();

app.MapHub<MarketHub>("/marketHub");

app.Run();
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Run server:

dotnet run
Enter fullscreen mode Exit fullscreen mode

Open browser:

http://localhost:5000
Enter fullscreen mode Exit fullscreen mode

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)