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.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
  • 1
    Have not tried it myself, but this video could be what you need: [Realtime notification using SignalR in asp net core mvc application](https://www.youtube.com/watch?v=8bqTmD5Tpjc) – Peter B Jun 10 '18 at 21:35
  • https://github.com/aspnet/SignalR/issues/182 – Lex Li Jun 10 '18 at 21:56

3 Answers3

23

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);
    }
}
live2
  • 3,771
  • 2
  • 37
  • 46
Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • 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 '18 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 '18 at 07: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 '18 at 20:57
  • 2
    Couple changes: `InvokeAsync` is now `SendAsync` (https://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 https://github.com/aspnet/SignalR/issues/2239 – Simon_Weaver Jun 06 '19 at 01: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

Stephu
  • 3,236
  • 4
  • 21
  • 33
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.

Mikael Mengistu
  • 332
  • 1
  • 6