Topic: Implementing Real Time Chat

I'm' thinking about adding one of those slide up buttons that says something along the lines of "Click to Chat with our Sales Associates".  To my knowledge there aren't any magic gems that handle this for us yet, so I was thinking of doing it this way..

rails g model ChatUser \
ip_address:string user_agent:string tracking_cookie:text

rails g scaffold Message \
chatroom_session:integer chat_user_id:integer

So then I can attribute all of the messages to a particular browser or store them in the database just in case the user gets disconnected and would like to start where they left off.  I've mocked up some .html.erb files and they look like this right now..

(chatroom_assistance.html.erb)

<link type=text/css rel='stylesheet' href='/stylesheets/chat.css'>

<div class="generic_h1">Customer Chat</div>

<div class="chatEncapsulator">
  <div id="chat-msgs">
    <p>One of our specialists will be with you momentarily.</p>
    <br>
  </div>
  
  <input id="real_time_interaction_msg" name="real_time_interaction[msg]" type="text" onkeypress="handleKeyPress(event,this.form)" value="">
</div>


<script>
  function getUniqueChatroomSessionId(){
    return 00000001;
  }
  
  function checkThatSessionIsUnqiue(){
  }
  
  function channelPullingLoop(sessionId){
    //  URL:  "/real_time_interaction/getData?sessonId=" + sessionId + "&lastSuccessfulPull=" + lastSuccessfulPull
    var newData = "GetDataFromAjaxFeed";
    
    if (newData != null){
      $('#chat-msgs').append("<p>" + newData + "</p>");
    }
    
  }
  
  
  var lastSuccessfulPull = "12:00";
  var uniqueChatroomSessionId = getUniqueChatroomSessionId();
  
  checkThatSessionIsUnqiue();
  
  setInterval("channelPullingLoop('" + uniqueChatroomSessionId + "')", 1000);
</script>

So down the road, that channelPullingLoop() will be pulling down data from rails via a route like

URL:  "/real_time_interaction/getData?sessonId=" + sessionId + "&lastSuccessfulPull=" + lastSuccessfulPull

So with the above URL, it would do a query for any messages made since lastSuccessfulPull, which would be a date time value. 

Does anyone know of any tuts that could help me with this project?  I actually don't know very much about cookies or if they can be stored in the database as text like I'm thinking, but yeah, this is where I am right now on things =/  I'm thinking tommorrow I'll caffinate and cludge through this problem, so any advice is appreciated.

Last edited by nsj (2012-10-22 16:29:34)

Re: Implementing Real Time Chat

I don't know if anyone's interested, but It's coming along really enjoyably.  The only problem I'm noticing is that this is extremely database intensive right now...  I think it would work out on a larger scale if I used web sockets or server-sent events

http://www.html5rocks.com/en/tutorials/ … ce/basics/
http://tenderlovemaking.com/2012/07/30/is-it-live.html

But the database is getting smashed right now, every simple ajax interval (once per second) queries the database for this...

if last_successful_pull == ""
      time_filter = Time.now - 60.minutes
    else
      last_successful_pull = last_successful_pull.to_time
      time_filter = last_successful_pull + 1.second 
    end

msgs = ChatMessage.find(
            :all, 
            :conditions => 
              ["created_at >= ? AND room_hash = ?", time_filter, room_hash])

which is less than ideal.  Is there a way that I can share the data between the two clients without going through the database?  It would be much more efficient if I could save up chat messages before dumping them into the database, and if I had a cache of messages, then I wouldn't even need my clients to read the database at any point since it would only need to pull them from memory.