15
votes

Is there a recommended approach to sharing a SignalR hub across two applications?

The actual situation is a public facing WebAPI project and an internal MVC WebApp. What I'd like to do is call methods on a SignalR hub from the WebAPI project and have the results of these methods pushed to clients connected via the MVC app.

Would the best option be to create a third 'Hub' project and have both projects connect to that? If so, how are the hub instances managed? Can both applications get a reference to the same hub from distinct app pools (and possibly hosts)?

I read a little bit about GlobalHost.ConnectionManager.GetHubContext, would this suffice to get a effectively a singleton hub which both apps could use?

Any thoughts greatly appreciated.

1
signalR supports CORS at different domains , so you could use 1 app to serve both apps.p.s. +1 nice question.Royi Namir
Would that imply that it'd be best placed to extract the Hub into a separate app and let two others call it? This was my suspicion as I couldn't think of a nice way to place it in either one of the existing projects and have them call each other.dougajmcdonald
Interesting stuff, we aim to deploy both the WebAPI and MVC app to Azure, but in the form of a VPN/VM with both sites running on the same VM. I'm not 100% sure whether this would count as a single server or not (would hope yes) so we may be able to avoid using a backplane approach, but would still need to share a hub across apps.dougajmcdonald
well , I can tell you that in the next few hours/tomorrow you'll get more appropriate answer from the sR team members which has much more knowledge than I have :-) I will keep tracking this question cuz i'd like to hear best practices for that issue.Royi Namir

1 Answers

4
votes

Just setup signalr message routing through a SQL Server table. It will automatically connect all hubs using the same routing setup. Nothing else to do, it's magic.

GlobalHost.DependencyResolver.UseSqlServer(ConfigurationManager.ConnectionStrings["signalr"].ConnectionString);

You will need the following nuget package: Microsoft.AspNet.SignalR.SqlServer