5

I'm developing a chat application using signalr and asp.net mvc 4. If a user added for first time - it will assign a connection id and he chat with other clients but if he refreshes his page he will assign a new connection id.

My question is how to maintain the connection id of the user on browser refresh?

Any help will be appreciated.

Sumit Kesarwani
  • 563
  • 2
  • 4
  • 22

3 Answers3

1

If a user is added for first time - it will assign a connection id that time you store in Session or some specific static variable if page will refresh that time check Session variable is empty or not if not pass the current id

if(Session["id"] != null )
{
   -- use last added id
}

I think this can be help you.

Mayank Patel
  • 117
  • 2
  • 15
0

I would add persistency via a DB or cache .

In my solution i added presidency via DB , for each session i assigned a unique identifier , which a treated as a signalR group, and on each browser refresh the client would register with that unique identifier , and i would assign the new connection id to the same unique identifier which is the group name. and sending message to the client is done via groups.

_signalrDispatcher.Clients.Groups("group name").SendData(data);
Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
0

In this answer I have explained how to manage connectionIds. The question was specific so I will summarize.

OnConnected and OnReconnected add user with connectionId (plus grup info if you have), but just check connectionId uniqueness (One user have many connectionIds at the same time).

OnDisconnected remove record/row with that connectionId. If user doesn't have any record in this group,that's mean user left the group. If user does not have any record left at all, that's mean user has disconnected.

You should use shared storage for keeping this records. Because if you have more than 1 instance, they need to share same data. I suggest you to use database, but you can use cache or session(if it's shared like redis).

I hope this help.

Community
  • 1
  • 1
Erkan Demirel
  • 4,302
  • 1
  • 25
  • 43