As you can imagine, it is possible to communicate with a hub, meaning to send messages to a hub. There are two possibilities:
- From the same web application
- From a different application
Let's study each of these.
Communication from the same web application
It is possible to send messages into a hub from the outside of SignalR. This does not mean accessing an instance of, for example, the ChatHub class, but only its connected clients. You can do this by injecting an instance of IHubContext<ChatHub> using the built-in dependency injection framework, shown as follows:
public class ChatController : Controller
{
private readonly IHubContext<ChatHub> _context;
public ChatController(IHubContext<ChatHub> context)
{
this._context = context;
}
[HttpGet("Send/{message}")]
public async Task<IActionResult> Send(string message)
...