Hi David,
I had made chat application using signalR just like facebook chat, Now I want to preserve the chat conversation data into database and want to display that historyfor its corrusponding chat room,Can you please guide how to archive that? Does signalR support communication with sql server?
Also Can you please guide how can I maintain the connection ID so that I can reinitialize the chat after the postback just like a facebook chat app.
First of all, if you are looking for help, just say you are looking for help. You don't know something, it doesn't mean SignalR is not supporting for it or it's an issue.
The key point is you don't care about the conn id. You got to map it with your own consistent unique key. Again, I would suggest not to maintain the conn id. It's better be random. If it's a must to maintain the conn id, then you can overwrite default conn id generator.
Hi ZeroHackeR,
Thanks for your reply, Does it means we can pass our own connection id ? and we can continue our chat after post back? I mean after reassigning all the details to the specific room will it continue the chat??
Here's the sample methods of the Chat server:
``` C#
public override System.Threading.Tasks.Task OnConnected()
{
// Get UserID. Assumed the user is logged before connecting to chat and userid is saved in session.
string UserID = (string)HttpContext.Current.Session["userid"];
// Get ChatHistory and call the client function. See below
this.GetHistory(UserID);
// Get ConnID
string ConnID = Context.ConnectionId;
// Save them in DB. You got to create a DB class to handle this. (Optional)
DB.UpdateConnID(UserID, ConnID);
}
private void GetHistory(UserID)
{
// Get Chat History from DB. You got to create a DB class to handle this.
string History = DB.GetChatHistory(UserID);
// Send Chat History to Client. You got to create chatHistory handler in Client side.
Clients.Caller.chatHistory(History );
}
// This method is to be called by Client
public void Chat(string Message)
{
// Get UserID. Assumed the user is logged before connecting to chat and userid is saved in session.
string UserID = (string)HttpContext.Current.Session["userid"];
// Save Chat in DB. You got to create a DB class to handle this
DB.SaveChat(UserID, Message);
// Send Chat Message to All connected Clients. You got to create chatMessage handler in Client side.
Clients.All.chatMessage(Message);
}
```
NOTE: I just wrote down here. Not tested yet.
Hi ZeroHackeR,
Again many thanks, Do you have any rough example? I am not expecting to make it working 100% I just want for the reference purpose.
Many Thanks
Can we take this to StackOverflow? This isn't an issue in SignalR and would benefit more in SO than here.
@niravpatel I wrote 3 methods just for you. Why asking for more? If you don't know how to save to DB, then you better go to a library and find some C# books. You should try first based on them, before asking. Lets end our topic here.
@raybooysen Totally agreed.
Closing
Most helpful comment
First of all, if you are looking for help, just say you are looking for help. You don't know something, it doesn't mean SignalR is not supporting for it or it's an issue.
The key point is you don't care about the conn id. You got to map it with your own consistent unique key. Again, I would suggest not to maintain the conn id. It's better be random. If it's a must to maintain the conn id, then you can overwrite default conn id generator.