user4779
user4779

Reputation: 817

What are the "method names" in hub connections?

I've scoured through the .NET documentation and cannot find what these strings representing methods mean. For instance "ReceiveMessage" and "SendMessage" in:

hubConnection = new HubConnectionBuilder();
...
hubConnection.On<string, string>("ReceiveMessage", ..);

and

await hubConnection.SendAsync("SendMessage", userInput, messageInput);

are some examples. I realize in the Hub we have methods that can be these names, but sometimes not? For the toy example I'm using from the .NET documentation, a ChatHub class is defined as follows:

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

So here I can see the "SendMesage" method exists. But nowhere is there any "ReceiveMessage" method in the source code. I'm a bit disappointed the documentation doesn't actually explain what these strings representing functions mean in any detail. Do they represent javascript functions? Only locally defined functions in C# (then where is ReceiveMessage?)? Globally defined functions in SignalR? What are they?

Upvotes: 2

Views: 1079

Answers (1)

Magnus
Magnus

Reputation: 18790

They refer to methods on the client.

I guess the exact details vary between languages, but here is one simple example in Dart/Flutter using the signalr_netcore package:

In your server code

// Call the TestMethod method on the client
await Clients.Caller.SendAsync("TestMethod", "some arguments from the server");

In your client code

hub.on('TestMethod', (arguments) async {
  print('TestMethod called from server with $arguments');
});

The above code will print TestMethod called from server with [some arguments from the server].

Upvotes: 1

Related Questions