SignalR is a technology that allows you to write bi-directional communication between a server and a client. This can lead to some pretty cool interactions, such as a live-updating dashboard on a website, or the ability to communicate requests to a server without building HTTP endpoints. SignalR does a lot of heavy lifting behind the scenes for you, including how the message is transmitted (it tries to use WebSockets, but, if they’re not supported, it will fall back to other methods automatically), and serialization / deserialization of message payloads. I tend to go out of my way to figure out ways to use it because it is just that neato.
Setup
In order to enable SignalR in an ASP.NET Core 2.2 web application – you just need to call services.AddSignalR() inside of the ConfigureServices method of your Startup, and register the routes to your Hubs in the Configure method.
Here’s a pretty simple/straightforward Startup.cs to illustrate:
Hubs
Hubs in SignalR are pretty simple to define. Just create a class, and inherit Hub. That’s basically it! The Hub itself can handle connection-specific events, such as when a new connection is made or a connection is disconnected, and here is where you define what methods the Hub can accept from its clients.
Sending Messages
You can send messages to connected clients in a variety of ways using the Clients property. Note that the SendAsync method takes the name of the method as a string. Later I’ll make another post about how to make a strongly-typed Hub, so you can avoid these magic strings (at least, on the server side).
All – Sends a message to every connected client
Caller – Sends a message back to the client that invoked the Hub method
Client – Sends a message directly to a client via its connection id
Others – Sends a message to everyone else except the client that invoked the Hub method
Group – Sends a message to all members of a group
Groups
As I mentioned above, it is possible to send messages to a group of connections. In order for this to work, you need to associate connection ids with a group. The Groups method lets you do this, simply by:
You might want to also put the Unsubscribe logic in the OnDisconnect handler, which would be fired whenever a connection is closed.
Accessing the Hub from outside the Hub
One thing that you will inevitably run in to is wanting to send messages to clients when you aren’t already in the Hub. Say that some other domain event happens and you want to notify the connections about it, but the event originates from some kind of automated process (that is, not from another connected client). It might be tempting to try and make the Hub a singleton so you can define methods on it, but this is not the right way!
IHubContext
Injecting IHubContext is the right way to go when you want to access a Hub. They are automatically registered in your dependency injection container when you called AddSignalR() in your Startup.cs file.
Once you inject IHubContext, you have access to the Clients property and can send messages just like if you were inside of a Hub.
SignalR Strongly-Typed Hubs – Darchuk.NET
[…] Previously I talked about how to set up the server side of SignalR for facilitating real-time commun…. One of the parts that I don’t particularly care for in that default implementation is that the hub methods (the messages that are sent to connected clients) are identified by strings. Sure, you could create some string constants to identify them, but that still leaves you without IntelliSense / knowing what parameters are expected when sending those messages. […]