Page 46 - MSDN Magazine, July 2018
P. 46

Cutting EdgE DINO ESPOSITO
Online Users, Streaming and Other SignalR Goodies
If you ever used any version of SignalR for the classic ASP.NET platform, you should be quite familiar with the concept of a hub. In SignalR a hub is the component that enables compatible client and server applications to arrange bidirectional remote procedure calls, from client to server and from server back to connected clients.
Concretely, in terms of software, a hub is a class that inherits from a system-provided base class and exposes endpoints for clients to call. Conceptually speaking, it has a few points in common with an ASP.NET controller. In particular, it’s the façade that receives client calls and reacts to them, and is organized around a relatively smallnumberofrelatedfunctions.InASP.NETCoreSignalR,the similarity between hubs and controllers is, in a way, even closer. This is my third article about ASP.NET Core SignalR for the Cutting Edge column, and in neither of the previous two arti- cles did I use a non-empty hub class. Explaining a bit more about SignalR hubs and how they’re implemented in ASP.NET Core is one of the purposes of this article. However, I’ll also touch on other interesting aspects of SignalR, specifically data streaming and the count of online users.
Hubs on ASP.NET Core SignalR
A hub is the entry point in a message pipeline through which connected clients and servers exchange messages. Hubs expose their methods as URLs and process any received parameters via model binding, in much the same way a canonical controller does. In a way, a hub is a dedicated controller that operates over two built-in protocols. The default protocol consists of a JSON object, but another binary protocol can be used that’s based on MessagePack. Note that in order to support MessagePack, browsers must support XHR Level 2. As Level 2 was introduced back in 2012, this might not be much of an issue today, but if your application requires support for very old browsers it might be worth noting. A quick browser check can be made here: caniuse.com/#feat=xhr2.
If any of the connected clients request a SignalR endpoint, the hub is directly invoked by the SignalR runtime engine. The conversation takesplaceovertheselectedprotocol,mostlylikelyWebSockets.In order to invoke the server, clients need to hold a connection object. A Web client would get it as follows:
var clockConnection = new signalR.HubConnection("/clockDemo"); clockConnection.start().then(() => {
clockConnection.invoke("now"); });
A client invokes a server endpoint via the “invoke” method on the connection object. Note that the exact syntax may vary depending
on the actual technology used for the client. The server replies through the methods defined on the base Hub class, and the con- versation takes place over the transport protocol of choice, most often WebSockets, like this:
public class ClockHub : Hub {
public Task Now() {
var now = DateTime.UtcNow.ToShortTimeString(); return Clients.All.SendAsync("now", now);
}
Note that you won’t be able to monitor the various calls using an HTTP tool like Fiddler. You need something like Chrome Developer Tools. In all the examples I wrote for my past columns on SignalR, however, I always used an empty hub class.
The hub class is the official SignalR façade for receiving client calls, and the fastest way for client/server communication to take place because of the dedicated pipeline. When the call occurs through the hub, the SignalR runtime can track and expose all the available information via the properties of the base Hub class. In this way, the SignalR connection ID and the entire context of the call, including any claims of the authenticated user, are available.
A hub is the entry point in a message pipeline through which connected clients and servers exchange messages.
In addition, through the hub, developers can handle connect and disconnect events. Any time a new connection is set up, or ceased, a hub method is called back. If you use SignalR only as a way to monitor remote long-running operations, then you canalsotriggerthetaskviaaplaincontrollerandinjectahub context in it for notifications. Or as an alternative, you can invoke the hub and trigger the task from within the hub. It’s your choice. SignalR works as an end-to-end framework, a bridge between the client and the server. Coding logic into the hub is acceptable as long as the work doesn’t go too deep into the layers of your code. Otherwise, go through the controller—whether MVC or Web API—and inject a hub context.
40 msdn magazine
}


































































































   44   45   46   47   48