Niels de Schrijver
Niels de Schrijver

Reputation: 335

What determines the SignalR connectionID and how to set it manually

I'm playing around with SignalR and was wandering how SignalR creates a connectionID is this based on my IP adress, the device I connect with or something else? And wether it is possible to set this ID manually. Let say I have a database with users which I give a unique number, can I use that number for the connectionID?

Kind regards

Upvotes: 2

Views: 2831

Answers (1)

scniro
scniro

Reputation: 16989

The SignalR connection Id is a generated guid. If you'd like to target users in a more meaningful way with your data, I found it useful to pass something with your client connection, such as a user Id and some other data from your database, along to the signalr hub and craft up a group based on what you provide. It should be a 1:1 mapping if you're trying to isolate users with your own identifiers.

you can do this by overriding OnConnected() in your hub, and implementing something like this, which would map the generated Id to your own. You can then target these groups (remember, 1:1, emulating an Id selector) to your liking.


public override Task OnConnected()
{
    var user = new User()
    {
        Id = Context.QueryString["Id"]
        Name = Context.QueryString["Name"]
    }

    Groups.Add(Context.ConnectionId, user.Id);

    return base.OnConnected();
}

Mapping SignalR Users to Connections goes into more detail as well.

Upvotes: 1

Related Questions