SignalR: How to get a current Hub instance

Once in a while, it’s helpful to call a Hub method from a controller, responding to some server side event.  Took me a while to figure this out, but in the end, this is easy.

DefaultHubManager hubManager = new DefaultHubManager(GlobalHost.DependencyResolver);
var hub = hubManager.ResolveHub("AdminHub"as AdminHub;
 
hub.SendMessage("woohoo");

One thing I don’t like is having to pass a hub name as a string – I think they should’ve asked for type. Easy enough to fix with a helper or extension method.

One thought on “SignalR: How to get a current Hub instance”

Leave a Reply

Your email address will not be published. Required fields are marked *