How to manage groups in SignalR core? (joining/leaving from all) - .net-core

I have some questions about SignalR.
An app scenario: An user can join/leave to many groups (NxN). But
those groups can be changed with a new request. So, how to remove an
user from all joined groups and add him to new list of groups? (Such
as: in first request i join A,B,C groups and with second request i
want to be in only groupS X,Z -i'm not listening a,b,c groups anymore-).
How to check a group name if it's already exists?
How to remove a group if it has no users/members in it? (garbage collector)
Hope someone helps me here!
(Signalr core: 2.2)

SignalR don't provide you the list of users that are in groups, how many groups there are and their names. So the logic that you need to implement is create for example a Dictionary so you can add there the name of your group and the users that are associated to that group. So when a request comes to change user from group A to group B you can do:
Lookup in what groups the user is.
Remove the user from the group.
Create the new group and add it to your Dictionary.
Add the user to the new group.
I believe this is a good aproach if you have one SignalR app/host because if you will have many instances of your signalR app, you can not access to the Dictionary to see if there is a user in some group in some other instance.

Related

How to create ChatRooms with Flutter/Firestore?

How do i go about creating chat groups where :
1- users can only join 1 group at a time.
2-that means users will not be able to see anything from that group without joining.
3- and useres have to exit existing group to join another.
4 also that means whatever been shared by users in that group will persist after user existing.
So I'm trying to create a session like functionality, but i have no idea how to implement that, any ideas?

.net core signalR, how to know the group name when a user leaves the group

I am working on a chat application.
As I understood, a single connection can join multiple groups.
And it works for me.
But when the user leaves one group, I will have to populate the existing user's name in that group.
so I think if I somehow know what is the group name that user left, I can remove that entry from the database.
public override async Task OnDisconnectedAsync(Exception exception){}
I assume this is the place where I have to do the thing.
BUT, how would I know the group name here?? Any ideas?

How to obtain list of common users from different groups in firebase DB

Is there any way I can find if users is present in both the groups here: user 1 so that notification/data can be sent to only that set of common users only?
As DB grows I think it will be inefficient to check if every user in one group is present in another or not.
Yes there is. You can create a list of users from GroupA, then create another list of users from GroupsB and then just simply use this line of code using Java8:
!Collections.disjoint(list1, list2);

How to use Session in SignalR connect

In my database a user has some area permissions.
What I want to do is to add this areas into Groups of SignalR.
So, when user sign in, I get this groups from database and save in Session["groups"] and when user connect to signalR, I use this session to add in Groups.
Something like this:
public Task Connect()
{
var groups = (string[])Session["groups"];
foreach (var group in groups)
{
Groups.Add(Context.ConnectionId, group);
}
}
How can I do this or something like?
As mentioned here SignalR Hubs - Managing groups
You can add connections to groups and send messages to particular groups. Groups are not persisted on the server so applications are responsible for keeping track of what connections are in what groups so things like group count can be achieved.
That means that you have to provide your own structure such as a Dictionary to hold your data.
For example, a Dictionary to hold group name as the key and List of areas as values (or the other way around if it suits you best).
So for each new area of your user, first update this Dictionary and then subscribe the user to the groups that were changed.
Groups.Add(Context.ConnectionId, group);
Nevertheless take a look at the link above and also here Topic based pub sub design patern. Do not hesitate because of the WCF on the title. Get a closer look on the way he implements the Filter class

Using IPrinciple.Identity.Name as a key in a dataBase to identify user's rows

I'm writing a small intranet app that uses Windows Authentication and Asp.Net MVC.
I need to store various bits of data in a db against each user.
As far as I can tell the IPrinciple object does not seem to have something like a unique id. So I was thinking I could just use User.Identity.Name as a unique value to identify rows in my db.
Is this a bad idea? Is there an alternative to this approach?
Thanks for any help.
I would create a User table that included an identity column as the id. When a person accesses the site, I would check the user table for that individuals unique id, and read it if it exists, or insert a new row if the user is new.
Login names can be long, and that could affect your indexes depending on the expected size of your data.

Resources