AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Usefullness of the methods in the hub class

Open OmiCron07 opened this issue 7 years ago • 1 comments

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.

OmiCron07 avatar Dec 02 '19 20:12 OmiCron07

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:

call-hub-methods-from-client

PersyJack avatar Feb 18 '25 19:02 PersyJack