How to set connection id in SignalR client?

Viewed 5956

I need to set connection id before hubConnection.start(); I look here https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client and what i found: connection.id - Gets or sets the client id for the current connection. Tried this. No working. Here is the client code:

let hubUrl = 'http://localhost:5000/chat';
let hubConnection = new signalR.HubConnectionBuilder()
        .withUrl(hubUrl)
    .configureLogging(signalR.LogLevel.Information)
    .build();
hubConnection.id = "12345";
hubConnection.on("Send", function (data) {
    let elem = document.createElement("p");
    elem.appendChild(document.createTextNode(data));
    let firstElem = document.getElementById("chatroom").firstChild;
    document.getElementById("chatroom").insertBefore(elem, firstElem);

});

document.getElementById("sendBtn").addEventListener("click", function (e) {
    let message = document.getElementById("message").value;
    hubConnection.invoke("Send", message);
});

hubConnection.start();

hubConnection.id = "12345"; this do not change real connection id. And maybe you are know some other way how to write client-side connection?

1 Answers

The connectionId can't be manually created or manipulated at all and it is created during negotiation request with the server. You can read here that you can implement your own IConnectionIdFactory but it is not recommended to do that. Maximum what you can do is get the connectionId and use it for mapping users and groups and other logic inside your hub.

Related