Usefullness of the methods in the hub class
In the class ClockHub, I would like to know the purpose of the method SendTimeToClients? It's never used anywhere. And if I want to use that method because I have business logic in there, how can I get a hub type exposing that method?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 1bbb2d1c-4dfe-3faf-d4b0-65ae1146961e
- Version Independent ID: 6b203f18-adfa-3a18-183d-b124325064c3
- Content: Host ASP.NET Core SignalR in background services
- Content Source: aspnetcore/signalr/background-services.md
- Product: aspnet-core
- Technology: aspnetcore-signalr
- GitHub Login: @bradygaster
- Microsoft Alias: bradyg
My understanding is that it's for clients to send message to the hub. The hub relays the message to another client or all clients. So a client can send messages by invoking a method in the hub:
await connection.InvokeAsync("SendMessage",
userTextBox.Text, messageTextBox.Text);
or receive messages by defining a method that listens to what the hub is broadcasting:
connection.On<string, string>("ReceiveMessage", (user, message) =>
{
this.Dispatcher.Invoke(() =>
{
var newMessage = $"{user}: {message}";
messagesList.Items.Add(newMessage);
});
});
For these examples the hub defines this method:
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
So "SendMessage" string inside InvokeAsync matches the method name in the hub and "ReceiveMessage" string in connection.On matches what the hub is broadcasting via SendAsync. Here is the link for the full explanation: