Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR error when invoking method on the server from JavaScript client

I have a C# server running my hub class, which contains only 1 method in there, which is as follows,

public class HothHub : Hub
{
  public async Task AddSingleUserGroup(string name)
  {
    await Groups.AddToGroupAsync(Context.ConnectionId, name);
  }
}

I also have a JavaScript client, which connects to the hub via the following code,

var connection;

async function signalRStart() {
    connection = new signalR.HubConnectionBuilder()
        .withUrl("https://somesignalrurl.com/hothhub", { withCredentials: false })
        .withAutomaticReconnect()
        .configureLogging(signalR.LogLevel.Information)
    .build();

    connection.on("hothHubToHothUpdate", () => {
        console.log("Called by the server!");
    });

    connection.onreconnecting(error => {
        console.log("Connection lost due to error " + error + ". Reconnecting.");
    });

    // Start the connection.
    await start();
}

async function start() {
    try {
        await connection.start();
        connection.invoke("addSingleUserGroup", "someUniqueUserName");
    } catch (err) {
        console.log(err);
        setTimeout(start, 5000);
    }
};

Now when the client initiates the connections and run start() on itself, this part seems to run fine. A connection to the signalR hub is made successfully. The problem I'm having is when connection.invoke("addSingleUserGroup", "someUniqueUserName"); is run although the error does not happen all the time. On first run, the method at the server end is hit successfully however, it looks like subsequent calls to it fail and this is the error returned in the client,

Uncaught (in promise) Error: Failed to invoke 'addSingleUserGroup' due to an error on the server. HubException: Method does not exist.
    at _callbacks.<computed> (signalr.js:1252:36)
    at HubConnection._processIncomingData (signalr.js:1364:33)
    at HubConnection.connection.onreceive (signalr.js:985:52)
    at webSocket.onmessage (signalr.js:2236:30)

I've read a few articles on here but most seemed to be related to the client calling the wrong method name having used a capital letter at the start of the method name when invoking it and some having mentioned issues with the method expecting 1 type parameter and receiving another type although in my instance here its hard to think how the server would not treat the incoming parameter as a string, which is what is being passed in. Has anyone got any ideas on what could be wrong here or what I could try? Thanks!

like image 492
Pradeep Patel Avatar asked Sep 15 '26 14:09

Pradeep Patel


1 Answers

Unfortunately I dont have an actual answer for this but after deploying the solution to my Azure App Service, the release version does not produce the error. It seems the error only persisted when in debug mode but like I said I'am not sure why.

like image 97
Pradeep Patel Avatar answered Sep 17 '26 02:09

Pradeep Patel