Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

How do I get a reference to a Hub? #182

Closed
mikebridge opened this issue Feb 7, 2017 · 9 comments
Closed

How do I get a reference to a Hub? #182

mikebridge opened this issue Feb 7, 2017 · 9 comments
Milestone

Comments

@mikebridge
Copy link

In the old SignalR I could get a reference to a hub via something like this:

    var hubManager = new DefaultHubManager(GlobalHost.DependencyResolver);
    var hub = hubManager.ResolveHub("myHub") as MyHub;

Is there a way to find a hub in the current version (0.2.0)?

@mikaelm12
Copy link
Contributor

What exactly are you trying to do here?

@mikebridge
Copy link
Author

mikebridge commented Feb 7, 2017

@mikaelm12 I'm trying to get some data from an Akka.NET actor to a SignalR hub, but I don't know how to get a reference to the hub....

Specifically, trying to port this

@BrennanConroy
Copy link
Member

Hubs aren't really meant to be called except from a client, you get away with it in your project by using GlobalHost.ConnectionManager.GetHubContext<CrawlHub>(); in your hub write method.

I would recommend creating and injecting a class with shared logic for use in your CrawlHub and SignalRActor, then in the shared class resolve an IHubContext<CrawlHub> for use in calls to write.

Arbitrary example (not tested)

namespace SharedHubLogic
{
    class HubMethods
    {
        private IHubContext<THub> _hubContext;
        public HubMethods(IHubContext<THub> hubContext)
        {
            _hubContext = hubContext;
        }

        public Task WriteMessageAsync(string method, param object[] args)
        {
            return _hubContext.Clients.All.InvokeAsync(method, args);
        }
    }
}

------------------------------------

class CrawlHub : Hub
{
    private HubMethods _hubMethods;
    public CrawlHub(HubMethods hubMethods)
    {
        _hubMethods = hubMethods;
    }

    public Task CrawlFailed(string reason)
    {
        return _hubMethods.WriteMessageAsync("writeStatus", reason);
    }
}

@mikebridge
Copy link
Author

@BrennanConroy Thanks, that looks like a better strategy. I'm just not sure how to get the reference to inject into the IHubContext if GlobalHost is no longer available....

@geirsagberg
Copy link

You should be able to resolve the IHubContext without doing any extra registration; SignalR does the necessary registration when calling .AddSignalR().

@davidfowl
Copy link
Member

@mikebridge You take it as a constructor argument to your registered service.

@ajtowf
Copy link

ajtowf commented Sep 16, 2017

In this screencast I setup an mvc controller, when posted to, we broadcast to all connected clients, in our specific case, it's angular apps running on a seperate host, so it involves CORS as well.

https://www.towfeek.se/2017/09/16/consuming-signalr-for-aspnet-core-from-angular/

Hope it helps, cheers!

@BrianChesbrough
Copy link

Arbitrary example (not tested)

namespace SharedHubLogic
{
    class HubMethods
    {
        private IHubContext<THub> _hubContext;
        public HubMethods(IHubContext<THub> hubContext)
        {
            _hubContext = hubContext;
        }

        public Task WriteMessageAsync(string method, param object[] args)
        {
            return _hubContext.Clients.All.InvokeAsync(method, args);
        }
    }
}

------------------------------------

class CrawlHub : Hub
{
    private HubMethods _hubMethods;
    public CrawlHub(HubMethods hubMethods)
    {
        _hubMethods = hubMethods;
    }

    public Task CrawlFailed(string reason)
    {
        return _hubMethods.WriteMessageAsync("writeStatus", reason);
    }
}

Anybody have a non-arbitrary, tested example? I've added something similar to this but now my App cannot connect to the server (works before I add the ctor to the hub).

@BrianChesbrough
Copy link

Arbitrary example (not tested)

namespace SharedHubLogic
{
    class HubMethods
    {
        private IHubContext<THub> _hubContext;
        public HubMethods(IHubContext<THub> hubContext)
        {
            _hubContext = hubContext;
        }

        public Task WriteMessageAsync(string method, param object[] args)
        {
            return _hubContext.Clients.All.InvokeAsync(method, args);
        }
    }
}

------------------------------------

class CrawlHub : Hub
{
    private HubMethods _hubMethods;
    public CrawlHub(HubMethods hubMethods)
    {
        _hubMethods = hubMethods;
    }

    public Task CrawlFailed(string reason)
    {
        return _hubMethods.WriteMessageAsync("writeStatus", reason);
    }
}

Anybody have a non-arbitrary, tested example? I've added something similar to this but now my App cannot connect to the server (works before I add the ctor to the hub).

Figured it out, the Hub constructor needed to take the context not the methods.

    private readonly HubMethods _hubMethods;

    public ChatHub(IHubContext<ChatHub> hubContext) => _hubMethods = new HubMethods(hubContext);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants