I am really looking for an advice
I have a chat app and I am thinking how to handle disconnecting, the problem is the users are of two types, anonymous and with accounts. The login is when you connect first time to the app I assign a random profile then I save in the localstorage so when you comeback your profile is persistent, then registered users, well will override the anonymous profile.
So when I close a tab, I refresh or close the browser, the OnDisconnectAsync
gets hit and I am not persistent with the ConnectionId
for any of the users at the moment. And is a problem if private chats are open between users, because ConnectionId
will change on page refresh
Connections are stored in this data type
private readonly Dictionary<T, HashSet<string>> _connections = new Dictionary<T, HashSet<string>>();
I have a few ideas and I don't know which one is the best:
- Prevent
_connections.Remove(username, Context.ConnectionId);
to happen, (maybe add a boolean for the status) and inOnConnectAsync
look after the username in localStorage and assign corresponding connectionId - Store the connectionId in localStorage and assign it on connect along with the username, but here is a chance to get duplicate connectionId's that will crash the app
- Store user profiles and connectionId in a database for registered users and anonymous users, check for an userId in localstorage and then connect with the connectionId from the database
- Still use in memory dictionary only but query it after the username first to get the connectionId, but I am gonna run into a problem with the UI, because I generate
ChatComponent
and the receiver when responds it will create a second, window for private chat to the other user. So I will need to have an object that containsreceiverName
,senderName
, and a reference to the component that is used for sending messages. - Maybe SignalR has a way to deal with this that I don't know and I should learn
I have been reading this post and I am still not sure because they are talking about zombie connections and I am gonna have to remove them too
I really don't have programmer friends to talk about this and all I got is litering here, I will delete the question later if is too dumb. Thank you.