Consider a web application like facebook, that can send realtime notifications to users.
What is the best way, using asp.net SignalR, to keep track of which connection ids belong to which user, even if the user disconnects, or reconnects later ?
In SignalR 2.0, this is done by using the inbuilt IPrincipal.Identity.Name , which is the logged in user identifier as set during the ASP.NET authentication. However, you may need to map the connection with the user using a different identifier instead of using the Identity.Name.
Adding and removing users To add or remove users from a group, you call the Add or Remove methods, and pass in the user's connection id and group's name as parameters. You do not need to manually remove a user from a group when the connection ends. The following example shows the Groups. Add and Groups.
Check out the following blog post:
Mapping ASP.NET SignalR Connections to Real Application Users
Briefly, you would be adding the connection ids to user on the OnConnected
method and remove that connection on the OnDisconnected
method. Keep in mind that an application user can have multiple connections. So, you need to have a one to many relationship between your user and the connection ids. The above linked post explains it in details with a sample.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With