Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping SignalR connections to users

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 ?

like image 760
Daniel Hursan Avatar asked Dec 25 '12 18:12

Daniel Hursan


People also ask

How do I send a SignalR to a specific user?

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.

How do I add a user to group SignalR?

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.


1 Answers

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.

like image 52
tugberk Avatar answered Sep 27 '22 17:09

tugberk