Tutorials SignalR & Real-Time .NET Applications
Server-to-Client Streaming: Sending large data chunks
On this page
Flowing Data
Sometimes you don't want to wait for a whole calculation to finish before showing results. Streaming allows the server to send data as it becomes available.
1. AsyncEnumerable Streaming
In modern .NET Hubs, you can return an IAsyncEnumerable<T>. SignalR will automatically stream each item to the client as it is yielded. This is perfect for high-frequency logs, progress reports, or searching across a massive dataset where you want to show 'live' results.
2. The Channel Pattern
For more complex scenarios, you can use System.Threading.Channels. Your Hub creates a channel and returns the Reader to the client. Background threads can then 'Write' into the channel, and the client will receive them in the exact order they were written. This is the 'Industrial' way to handle streaming in .NET.
3. Architect Insight
Q: "When should I use streaming instead of standard Hub methods?"
Architect Answer: "Use streaming when the total payload is large or the generation time is long. Large payloads in a single SendAsync can block the Hub for other users. Streaming breaks that payload into small, manageable 'Frames', keeping the connection healthy and the UI responsive."