Click here to Skip to main content
15,898,881 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i.m new developer using library SignalR in my MVC Project , i need help to know how can i save chat history in database sql?
Posted
Comments
Wendelius 22-Feb-15 8:43am    
This is a far too big question to be answered in a single solution. Try to break down the problem, post a question concerning only one subject and state what you have already done.
Member 11470451 22-Feb-15 9:37am    
hi Mika , first thx for ur replay
am just have web site its has chat by signalr
i need only save the any chatting happen in the chathub , to my table in db
automatically
Sergey Alexandrovich Kryukov 22-Feb-15 8:44am    
How one SignalR is related to database? You need to embrace separation of concerns, or "separate and conquer" conception.
—SA
Member 11470451 22-Feb-15 14:25pm    
i use chat between me and someone i need to save chat history at table in databas
how can i make it with sql server
Sergey Alexandrovich Kryukov 22-Feb-15 16:52pm    
Please see what Dave Kreskowiak wrote. You are repeating the same thing. What do you mean "how"? By doing appropriate software development work. If this is a problem, learn software development. Repeating the same thing without pointing our real problem is not helping you much.
—SA

1 solution

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()
    {
          // 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);  
    }

For more information, please follow below.

https://github.com/SignalR/SignalR/issues/1214

Hope that helps, thanks.
 
Share this answer
 
Comments
SaranshSrivastava 24-Feb-15 13:27pm    
please do not downvote the answer; if you think it's a correct answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900