Search code examples
asp.net-mvc-4streamingiis-7.5signalrlong-polling

Using ASP .Net SignalR for Long Polling for a single client?


I have this problem in which I need to compress (zip) multiple files from the web app (on the server) before streaming it down to the browser. I'm pulling the files from a separate service that connects to a SQL database. So there's a huge delay in opening the files from the service as well as a delay in compressing the files before the zipped package can be streamed to the browser. Ideally, I would like to have the DOWNLOAD button on the page make a call to a SignalR method on the server which will then push a notification back to the client once the multiple files are done compressing. That way, the browser won't request the server to stream the zipped file right away. It will only begin streaming once the multiple files are done compressing.

background info: I'm using IIS 7.5 and MVC 4.

I've been reading up and watching videos on SignalR, but have only seen examples of chat hubs and pushing to multiple clients, etc. Would it be possible to only use SignalR for the client that is making the request? And if so, I would appreciate some example code or perhaps a link to a tutorial on how one could accomplish something like this. Thanks!


Solution

  • To achieve what you need, you will have to define 3 clients

    • The Browser, it will call The Hub when a download is requested, then it will wait for a call from The Hub to download the files.
    • The Server, receives a notification from The Hub when the browser requests a download, and when all is ready calls The Hub to pass the files.
    • The Service, received the files from The Hub when its passed from The Server, and make the files ready for download, then send a notification to The Hub to inform The Browser.

    Note

    Storing large files in memory is not recommended, and passing it through SignalR is not as well, unless its the only way the server and the service can share the files, so if you have a common storage -Disk or Database- then its better to use it