Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Want to save chat conversation in sql server but not supporting #1214

Closed
niravpatel opened this issue Dec 31, 2012 · 8 comments
Closed

Want to save chat conversation in sql server but not supporting #1214

niravpatel opened this issue Dec 31, 2012 · 8 comments

Comments

@niravpatel
Copy link

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.

@ZeroHackeR
Copy link

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.

  • Since you were saying something like FB chat, I assumed you already have your user log-in state management (userid in session or cookie or whatever).
  • I suggest you use HUB.
  • Once a user logged in and activates chat, then you start HUB connection and get SignalR's connection id.
  • Since you already have userid of the connected user, save the mapping of SignalR's connection id and userid. (eg: Table Structure: user_id, conn_id, updated_date).
  • when you received chat message from a connection id, then you can easily look up from which user it came from.
  • Then, save the chat log using userid and retrieve the log using userid as well.

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.

@niravpatel
Copy link
Author

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??

@ZeroHackeR
Copy link

  • I meant, once a user logged in, you would have his/her userid, right?
  • So, you save the userid in your session variable. Eg: in your app log in page, after a user is authenticated, save his userid in session. Eg: Session["userid"] = "123";
  • Then, once user connected to chat, SignalR will generate a Connection ID. You may save the conn id and user id mapping to the DB as well (Optional, but it will be useful for verification and data mining. For the sake of simplicity, I will not talk about it here)

Here's the sample methods of the Chat server:

    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.

@niravpatel
Copy link
Author

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

@raybooysen
Copy link

Can we take this to StackOverflow? This isn't an issue in SignalR and would benefit more in SO than here.

@ZeroHackeR
Copy link

@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.

@davidfowl
Copy link
Member

Closing

@amehinansary
Copy link

@ZeroHackeR do u mean by saving chat that i can save the whole chat at once? am looking for some help and guide as i want to save the whole chat in sql table after one of two users chatting is disconnected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants