Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current hub in SignalR

Tags:

signalr

Is there a good way to call methods in SignalR hub from a controller ?

Right now I have this:

public class StatsHub : Hub
{
    private static readonly Lazy<StatsHub> instance = new Lazy<StatsHub>(() => new StatsHub());
    public static StatsHub Instance { get { return instance.Value; } }

    public StatsHub()
    {
        if (this.Clients == null)
        {
            var hubContext = SignalR.GlobalHost.ConnectionManager.GetHubContext<StatsHub>();
            this.Clients = hubContext.Clients;
            this.Groups = hubContext.Groups;
        }
    }

    // methods here...
}

so in my controller actions I can just say, for example

StatsHub.Instance.SendMessage("blah");

and it's almost good, except that hubContext doesn't have Caller or Context properties of Hub - which are nice to have.

Hopefully, there's a better way to do this ?

like image 997
Evgeni Avatar asked Sep 15 '12 23:09

Evgeni


People also ask

What is Hub in SignalR?

Hubs are SignalR's high level API that allow both realtime client-to-server and server-to-client RPC over HTTP. The hub supports 1-to-many RPC e.g.: all clients, groups of clients, just the caller etc. Transport is over one of the following (best to worst): WebSocket, server sent events, forever frame, long polling.

How do I add a class to hub SignalR?

Or you can also add SignalR to a project by opening the "Tools" | "Library Package Manager" | "Package Manager Console" and running the command: "install-package Microsoft. AspNet. SignalR". Now again add the SignalR class.

Is SignalR asynchronous?

SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.


2 Answers

If you want to broadcast over a hub from outside of the hub, you need GlobalHost.ConnectionManager.GetHubContext<MyHub>() to get ahold of the hub context. You can then use this context to broadcast via the .Clients property.

As indicated in your sample code you already get ahold of the hub context, but doing so inside the hub just doesn't feel right in my opinion. If you're only using the logic in SendMessage() from your controller actions, I'd move the code right into the controller action and use the hub context obtained via GetHubContext<T>() from there.

Please note that the Caller or Context property will always be null in this scenario, because SignalR wasn't involved when making a request to the server and therefore cannot provide the properties.

like image 171
Alexander Köplinger Avatar answered Sep 19 '22 00:09

Alexander Köplinger


Found a DefaultHubManager, which is what I need, I think.

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

Works. If there's an even better/preferred way - please share.

like image 38
Evgeni Avatar answered Sep 22 '22 00:09

Evgeni