For this issue, I think you should save the user id and the connection id to your database as well as save the messages that are sent in that chat.
Just check below code.
public override System.Threading.Tasks.Task OnConnected()
{
string UserID = (string)HttpContext.Current.Session["userid"];
this.GetHistory(UserID);
string ConnID = Context.ConnectionId;
DB.UpdateConnID(UserID, ConnID);
}
private void GetHistory(UserID)
{
string History = DB.GetChatHistory(UserID);
Clients.Caller.chatHistory(History );
}
public void Chat(string Message)
{
string UserID = (string)HttpContext.Current.Session["userid"];
DB.SaveChat(UserID, Message);
Clients.All.chatMessage(Message);
}
For more information, please follow below.
https://github.com/SignalR/SignalR/issues/1214
Hope that helps, thanks.