Search code examples
asp.net-web-apiarchitecturewindows-servicessignalrnservicebus

Using SignalR to push to clients from a long running process


Firstly, here is state of my application:

enter image description here

I have a request coming in from a client (angularjs app) into my API (web api 2). This request is processed and a record is stored in a database. A response is then sent back to the client.

Currently, I have a windows service polling and processing this record(s). Processing this record can be long running. As a side effect to processing this record, there might be notifications generated to be sent back to one or more clients.

My question is how do I architect this, such that I can utilise SignalR to be able to push the notifications back to the client.

My stumbling block:

I can register and store (in-memory backed by a db) the client's SignalR connectionid along with the application's own user identifier. This way I can match a generated notification with a signalr client.

At the moment, I'm hosting the SignalR hubs within the IIS process. So how do I get back from the Windows Service to IIS to notify the client when a notification is generated?

Furthermore, I should say I am already using SignalR elsewhere in the application and am using a SQL Server backplane.

The issue's with the current architecture:

  • Any processing is done in the same web request, and notifications are sent out via SignalR before a response to the client is returned. Luckily, the processing is minimal and very quick.

  • I think this is not very good in terms of performance or maintenance in the long run.

Potential solutions:

  • Remove SignalR hubs from IIS and host them somewhere else - windows service?
  • Expose an endpoint on the API to for the windows service to call to push the notification once a notification is generated?

Finally, to add more ingredients to the mix: Use a service bus to remove the polling component of the windows service, and move to a pub/sub architecture. Although this is more work than I want to chew off right now.

Any ideas/recommendations/constructive criticisms are welcome.

Thanks.


Solution

  • Take a look at this sample for starters

    Another more advanced solution can be using a backplane to manage the communications between the front end and the backend...

    HTH