how to get SignalR user connection id out side the hub class? how to get SignalR user connection id out side the hub class? asp.net asp.net

how to get SignalR user connection id out side the hub class?


Yep. You can use $.connection.hub.id.


There's another way also, you can get connection id into your controller from hub by invoking a method of hub and you can return the required ID from there.

Controller Code

var HubContext = GlobalHost.ConnectionManager.GetHubContext<"ChatHub">(); //`ChatHub` can be your Hub NameChatHub HubObj= new ChatHub();var RequiredId= HubObj.InvokeHubMethod();

Code inside Hub

public string InvokeHubMethod(){     return "ConnectionID"  //ConnectionID will the Id as string that you want outside the hub}


This works for me:

var hub = $.connection.someHub;// After connection is startedconsole.log(hub.connection.id);