Stavros_S
Stavros_S

Reputation: 2235

How to broadcast events from Web Api project to MVC app on a different App Pool using SignalR

My windows server running IIS has an MVC application in one app pool and a Web Api project in another. These projects were developed in different solutions. The web app currently communicates with the Web Api to use as a rest service. I need to add a SignalR hub to the Web Api project so that it can broadcast some data to the web app in certain situations.

How can I go about doing this? I have read in other posts that you need to use Sql server as a backplane but I'm not sure that is necessary since the two apps are on the same server. If this is the case though, how does the Web app pick up the broadcast when the hub code is not in the project?

Upvotes: 2

Views: 1531

Answers (1)

BMac
BMac

Reputation: 2230

Since you are only using a single server, I would stay away from backbone scale out (i.e. SQL Server message bus, redis etc) until you are forced to scale out to multiple servers, in my experience SignalR scale out can be painful for multiple reasons.

Because you have the solutions in different app pools, you will need to think of one as the "SignalR Server" and the other as a "Client". I would pick the Web API as the server project and the web app as the client.

In order to communicate from the "Server" to the "Client" you should treat the web app as any other .net client (i.e. you can use a console app as a signalR client). At start up, or at some other event in your web app you will have to register with the "Server"(web api) that you are listening out for messages that signalR has to send. There is many great tutorials on this over at Asp.net, at the time of writing this link explains how you could go about setting all this up, I have extreamly simplified this tutorial below:

1). install Microsoft.AspNet.SignalR.Client from Nuget

2). register with the hub and start an async connection:

var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price));
await hubConnection.Start();

Upvotes: 4

Related Questions