SignalR icon indicating copy to clipboard operation
SignalR copied to clipboard

How to use Streaming ?

Open AmrTawfek opened this issue 9 years ago • 9 comments

Details : I need an example to demonstrate how the streaming is working in SignalR core The client : C# Console Application

AmrTawfek avatar Oct 13 '17 11:10 AmrTawfek

Here is an example of streaming from the C# client https://github.com/aspnet/SignalR-samples/blob/d5fdaea0759079c0a1304b57ac625fcc457377ae/StockTickR/CsharpClient/Program.cs#L64

BrennanConroy avatar Oct 13 '17 14:10 BrennanConroy

thanks also what about server side code ?

AmrTawfek avatar Oct 13 '17 14:10 AmrTawfek

Heres the Hub https://github.com/aspnet/SignalR-samples/blob/d5fdaea0759079c0a1304b57ac625fcc457377ae/StockTickR/StockTickRApp/StockTickerHub.cs#L22 Which calls into https://github.com/aspnet/SignalR-samples/blob/d5fdaea0759079c0a1304b57ac625fcc457377ae/StockTickR/StockTickRApp/StockTicker.cs#L52

BrennanConroy avatar Oct 13 '17 14:10 BrennanConroy

Another example for different usage https://github.com/aspnet/SignalR/blob/dev/samples/SocketsSample/Hubs/Streaming.cs

BrennanConroy avatar Oct 13 '17 14:10 BrennanConroy

Is this suitable for streaming large files (GBs+)? I thought about making small chunks of the file and open a stream for each of the chunks, is this a good idea?

weitzhandler avatar Dec 10 '17 01:12 weitzhandler

I would not recommend using streaming for large files. The streaming function is generally designed for allowing the server to send a series of values over time, rather than sending chunks of a single value.

It's certainly possible to use that functionality to stream a large file, and in the MsgPack protocol the overhead would be pretty low (in JSON, it would be a very inefficient transfer as every chunk would have to be base64 encoded and decoded again on the client). However, it's not really designed for sending large data. If you need to send large data, better to send an URL to the client over SignalR and have the client do a standard HTTP request for the data.

analogrelay avatar Dec 10 '17 23:12 analogrelay

@anurse Thanks for your response. I'm sorry but I didn't make my self clear, I need to opposite: upload files from a WPF client to an ASP.NET Core server. Note that I don't want to use other protocols other than HTTP (i.e. FTP).

  • How about transferring a FileStream from client to server controller-action or via SignalR?
  • Would chunking up the files and send a stream to each chunk in the client be beneficial?

weitzhandler avatar Dec 11 '17 00:12 weitzhandler

There is no support for client-to-server streaming in SignalR Core and it's not on our roadmap for the first release. We've thought about it a few times, but the design is quite a bit more complicated than from server-to-client.

So, you'd basically have to do this on your own (built your own "SendChunk" hub method that lets you chunk the data yourself). At that point, you might as well just use HTTP since you aren't really gaining anything from SignalR. There's no reason a multi-GB file upload via HTTP would be any less reliable than trying to stream it using SignalR. Even the server-to-client streaming doesn't have any way to "resume" existing streams after a network interruption, so you'd have to write all that yourself. Most large file upload processes these days are done via HTTP, so there are plenty of patterns and resources out there to help make them reliable, and I'd encourage you to use those rather than trying to invent new things on top of SignalR (which isn't designed for it at all)

analogrelay avatar Dec 11 '17 17:12 analogrelay

Thank you!

weitzhandler avatar Dec 11 '17 17:12 weitzhandler