Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get connectionId outside of Hub, SignalR

How do I get the clients connectionId/clientId outside of the Hub?.. I have managed to do the following:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();

But in that context-object there is no such thing as a clientId.

like image 920
Inx Avatar asked May 15 '12 12:05

Inx


People also ask

How do you get SignalR user connection ID outside the Hub class?

You can invoke a method inside the hub and you can return the connection ID from there. You'll get required ID outside the hub in connectedUserID Variable. Hope This Helps.

Is SignalR connection ID unique?

Each client connecting to a hub passes a unique connection id. You can retrieve this value in the Context. ConnectionId property of the hub context.

What is a SignalR hub?

What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.


2 Answers

You could implement IConnected/IDisconnect on the Hub and manually keep track of clients for example in a database, then pull back the list when required. The example below is from the SignalR Wiki

public class Status : Hub, IDisconnect, IConnected
{
    public Task Disconnect()
    {
        return Clients.leave(Context.ConnectionId, DateTime.Now.ToString());
    }

    public Task Connect()
    {
        return Clients.joined(Context.ConnectionId, DateTime.Now.ToString());
    }

    public Task Reconnect(IEnumerable<string> groups)
    {
        return Clients.rejoined(Context.ConnectionId, DateTime.Now.ToString());
    }
}
like image 138
WooHoo Avatar answered Oct 16 '22 10:10

WooHoo


Why would there be a connection Id on the global context? Which connection would it be in reference to? When you get the global context, you are accessing a one way channel from server to client and can send messages over it. You don't have access to the connection id of the hub since you aren't calling into it. You can store them somewhere in your application if you need to use them.

like image 4
davidfowl Avatar answered Oct 16 '22 10:10

davidfowl