Using HubContext to Send Messages from Outside SignalR Hub
Understand how HubContext can be used in our application in the context of a distributed SignalR Hub.
Overview
SignalR comes with the IHubContext interface, the implementations of which allow us to access all fields of a specific SignalR Hub, including its groups, clients, and so on. We don’t have to worry about registering any concrete implementations of this interface ourselves. If we inject this interface into the constructor of any class, an appropriate implementation will be automatically resolved. All the dependencies get automatically registered when you call the AddSignalR
method on the Services
field of the builder
object inside the Program.cs
class.
HubContext is not designed specifically for a scaled SignalR Hub. We can use it with monolithic SignalR Hubs, too. But, because it’s especially useful in the context of a distributed SignalR Hub, we'll talk about it in this section.
Implementing HubContext in our application
To demonstrate how HubContext works, let's open the HomeController.cs
file, which is located in the Controllers
folder of the SignalRServer
project.
The path for the directory is given below to get into the HomeController.cs
file.
Get hands-on with 1400+ tech skills courses.