26

I've successfully setup a SignalR server and client using the newly released ASP.NET Core 2.1. I built a chat room by making my ChatHub extend Hub: whenever a message comes in from a client, the server blasts it back out via Clients.Others.

What I do not yet understand is how to send a message to clients not as a response to an incoming message. If the server is doing work and produces a result, how do I gain access to the Hub in order to message particular clients? (Or do I even need access to the Hub? Is there another way to send messages?)

Searching this issue is difficult as most results come from old versions of ASP.NET and SignalR.

2

3 Answers 3

24

You can inject the IHubContext<T> class into a service and call the clients using that.

public class NotifyService
{
    private readonly IHubContext<ChatHub> _hub;

    public NotifyService(IHubContext<ChatHub> hub)
    {
        _hub = hub;
    }

    public Task SendNotificationAsync(string message)
    {
        return _hub.Clients.All.SendAsync("ReceiveMessage", message);
    }
}

Now you can inject the NotifyService into your class and send messages to all clients:

public class SomeClass
{
    private readonly NotifyService _service;

    public SomeClass(NotifyService service)
    {
        _service = service;
    }

    public Task Send(string message)
    {
        return _service.SendNotificationAsync(message);
    }
}
4
  • I may be a bit naive with regard to ASP here, but how does that service help me send a message? If I have some HandleEvent function out elsewhere in my code (outside most of ASP land), how would I access this service, instantiate it, and use it?
    – TheBuzzSaw
    Jun 14, 2018 at 19:45
  • Based on your question I thought you were trying to send a message from a different part of an ASP app. If you are trying to do if from a different project/application then you would need to call an API to send the message for you. If you have some example code of what you are trying to do that might help make things clearer
    – Simply Ged
    Jun 15, 2018 at 7:01
  • I fixed my understanding of how ASP.NET services work, and now this answer makes significantly more sense. I think this is what I need.
    – TheBuzzSaw
    Jun 22, 2018 at 20:57
  • 2
    Couple changes: InvokeAsync is now SendAsync (github.com/aspnet/SignalR/issues/1300) and make sure to add the namespace using Microsoft.AspNetCore.SignalR; in order to get the extension methods needed. See also github.com/aspnet/SignalR/issues/2239 Jun 6, 2019 at 1:36
4

Simple inject the hubcontext into the class where you use the hubcontext.

Details you will find there:

Call SignalR Core Hub method from Controller

2

There are now official Microsoft Docs for the SignalR HubContext that answer your question https://learn.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.1

But yes, as others have pointed out, you need to get an instance of IHubContext via dependency injection to access hub methods outside of the hub.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.