Search code examples
javascriptc#asp.netsignalrsignalr-hub

How do i pass a parameter to a function im calling in the SignalR connection.invoke method?


I need to pass some data to the function i wanna render, but i dont know the syntax to do so.

Code:

var connection = new signalR.HubConnectionBuilder().withUrl("/hubFilas").build();

connection.start().then(function () {
    var time = setInterval(function () {
        connection.invoke("renderComponent").then(function (o) { DisplayHub(o) }).catch(function (err) {
            return console.error(err.toString());
        });
    }, 120000)

Any ideas?


Solution

  • You just pass the data as parameter to the invoke call. As described in the docs for sample hub:

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

    You can invoke the method via following call:

    connection.invoke("SendMessage", user, message);
    

    Also check out: