Introduction
In this article, we'll learn how to use Dynamic Hub in ASPNET Core SignalR. The newly written SignalR allows you to write a dynamic type of T Hub. The benefit of using dynamic hub is that the method will resolve to the runtime so that you can register any method at runtime and call it.
This article is part of series of posts on SignalR using ASPNET Core. You can read:
- Overview of New stack SIgnalR on ASPNET Core here
- Getting Started With SignalR Using ASP.NET Core And Angular 5 here
- Working with Azure service
- Working with Xamarin
- Working with streaming data here
This article demonstrates the following tasks:
- Creating Dynamic Hub
- Creating T of Hub
- Consuming the Dynamic and T of Hub by the client
- Demo
The source code is available at GitHub.
Before reading this article, I would highly recommend you to go through the other articles of the series which are mentioned above.
Creating Dynamic Hub
This is a base class for SignalR hubs that uses dynamic keyword to represent client invocations. Basically, the idea behind Dynamic Hub is hidden in the dynamic keyword. DynamicHub is inherited from Hub class. This is an abstract class and it is implementing the DynamicHubClients which contains all the dynamic type properties.
From MSDN
"The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at runtime. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM)."
A Hub contains the following properties.
- Clients : A clients caller abstraction for a hub, where you can push message to all connected clients
- Context : A context abstraction for accessing information about the hub caller connection. It contains the connection and claims principle information.
- Groups : A manager abstraction for adding and removing connections from groups. Ease of managing clients grouping.
- Events : It provide the event notification on the client connected or disconnected so that you can override these events and add your custom login.
Dynamic Hub class
- //https://github.com/aspnet/SignalR/blob/dev/src/Microsoft.AspNetCore.SignalR.Core/DynamicHub.cs
- public abstract class DynamicHub : Hub
- {
- private DynamicHubClients _clients;
-
-
-
-
- public new DynamicHubClients Clients
- {
- get
- {
- if (_clients == null)
- {
- _clients = new DynamicHubClients(base.Clients);
- }
-
- return _clients;
- }
- set => _clients = value;
- }
- }
DynamicHubClients Class
-
-
-
-
-
- public class DynamicHubClients
- {
- private readonly IHubCallerClients _clients;
-
-
-
-
-
- public DynamicHubClients(IHubCallerClients clients)
- {
- _clients = clients;
- }
-
-
-
-
-
- public dynamic All => new DynamicClientProxy(_clients.All);
-
-
-
-
-
-
- public dynamic AllExcept(IReadOnlyList<string> excludedConnectionIds) => new DynamicClientProxy(_clients.AllExcept(excludedConnectionIds));
-
-
-
-
- public dynamic Caller => new DynamicClientProxy(_clients.Caller);
-
-
-
-
-
-
- public dynamic Client(string connectionId) => new DynamicClientProxy(_clients.Client(connectionId));
-
-
-
-
-
-
- public dynamic Clients(IReadOnlyList<string> connectionIds) => new DynamicClientProxy(_clients.Clients(connectionIds));
-
-
-
-
-
-
- public dynamic Group(string groupName) => new DynamicClientProxy(_clients.Group(groupName));
-
-
-
-
-
-
- public dynamic Groups(IReadOnlyList<string> groupNames) => new DynamicClientProxy(_clients.Groups(groupNames));
-
-
-
-
-
-
-
- public dynamic GroupExcept(string groupName, IReadOnlyList<string> excludedConnectionIds) => new DynamicClientProxy(_clients.GroupExcept(groupName, excludedConnectionIds));
-
-
-
-
-
- public dynamic OthersInGroup(string groupName) => new DynamicClientProxy(_clients.OthersInGroup(groupName));
-
-
-
-
- public dynamic Others => new DynamicClientProxy(_clients.Others);
-
-
-
-
-
-
- public dynamic User(string userId) => new DynamicClientProxy(_clients.User(userId));
-
-
-
-
-
-
- public dynamic Users(IReadOnlyList<string> userIds) => new DynamicClientProxy(_clients.Users(userIds));
- }
Dynamic Chat Hub Class
In this hub, we are just sending a message to the client so in the case of Hub, we have to call SendAsync by passing the client method name which is listening to the client and message or other data to send. In case of Dynamic Hub, we can call any method which is bypassed by the compiler because it has dynamic keyword. But that method must be listened to on the client side to receive the data like Send method in above class.
- public class DynamicChatHub : DynamicHub
- {
- public async Task SendMessage(ChatMessage message)
- {
- await Clients.All.Send(message);
- }
- }
I have created a separate service and component for dynamic hub which is consuming Dynamic Hub for communication and showing the messages. You will find the same in the attached project.
Creating T of Hub
Hub<T> is a base class for a strongly typed SignalR hub where T is type of client. The T typed can be used to invoke a method on clients connected. If you want your strongly typed custom methods, this is for you.
In a Hub, we have to tell in which method client is listening, so that while writing the name of the client method, you can avoid typos and other issues.
We are going to create an Interface called IChatClient.
- public interface IChatClient
- {
- Task Send(string message);
- }
TChatHub Class
This class is Inherited from strongly typed IChatClient Hub<IChatClient>. In this hub class, you can call IChatClient's methods on the client. This is the beauty of this Stack. This is just for pushing messages to the connected client. You can send a custom object to the client like we did in the streaming article earlier here.
- public class TChatHub : Hub<IChatClient>
- {
-
- public Task Send(string message)
- {
- return Clients.All.Send(message);
- }
-
- }
Finally, we are done with Dynamic and T typed hub in SignalR.
Summary
In this, we have seen the working demo of dynamic and typed hub with signalR using ASP.NET Core and Angular 5. We have learned the following.
- Dynamic Hub
Dynamic Hub uses the dynamic keyword, we all know the beauty of dynamic keyword.
- Typed Hub
A Base class for a strongly typed SignalR hub.
References