SignalR and ASP.NET MVC: Integration in business layer

1.6k views Asked by At

our application is divided in several layers, the most important are:

  • Data Layer
  • Business Layer
  • Presentation Layer (ASP.NET MVC 5)

The Business Layer must live broadcast some information to the connected clients via SignalR (Presentation Layer). What is the best practice for doing that?

Should the Business layer use OWIN to self host a SignalR Server? Or is it cleaner, if the presentation layer defines a webservice (which holds the signalr logic) that is called by Business Layer to push Information to presentation layer?

Are there any better ideas?

Thanks a lot Levi

2

There are 2 answers

0
Anders On

I created a Event Aggregator (Service bus) proxy for SignalR, this way you can fire a event anywhere in your domain (Business, data etc) and it will be forwarded to your clients.

Install using nuget

Install-Package SignalR.EventAggregatorProxy 

Follow the wiki for the few steps needed for setting it up. https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki

Once configured you can listen to events from javascript

ViewModel = function() {
   signalR.eventAggregator.subscribe(MyApp.Events.TestEvent, this.onTestEvent, this);
};

demo project:

https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/tree/master/SignalR.EventAggregatorProxy.Demo.MVC4

0
Teddy On

Just Install SignalR in your business layer

Install-Package Microsoft.AspNet.SignalR

And...

GlobalHost.ConnectionManager.GetHubContext<BroadcastHub>().Clients.All.Broadcast(message);

*BroadcastHub could can be empty like

public class NotificationHub : Hub
{

}

*message is your broadcast string message

This worked for me just fine