Reputation: 26
Good day. I am really new with Rails and I am trying to find the way how to auto-update the message list in chat window.
I'll describe what I got:
<input type="text">
which is saved to the mysql db each time I press Send button.What I want to do is: while 2 people are typing/sending their messages both will got auto-update from db without refreshing the page or pressing Send button. If user_1 sends then user_2 recieves it automatically.
I need some how to refresh this variables:
<%=@date[i][1]%>
<%=@message[i]%>
I have read about AJAX long poll and WebSockets, but I understand nothing.
main.html.erb:
<table class="table">
<thead>
<tr>
<th style="width: 10%">Time</th>
<th style="width: 90%">Message</th>
</tr>
</thead>
<tbody>
<% for i in 0..4 %>
<tr>
<td ><div class="well well-sm"><%=@date[i][1]%></div></td>
<td ><div class="well well-sm"><%=@message[i]%></div></td>
</tr>
<% end %>
</tbody>
</table>
Controller:
def main
if(session[:isLogin]!=true)
redirect_to :controller => 'auth', :action => 'index'
end
@mess_count = 5
@output = Chat.last(@mess_count)
@message = []
@date = []
for i in 0..@mess_count-1
@date[i] = @output[i][:date].to_s.split(' ')
@message[i] = @output[i][:message]
end
P.S. I know that my code sucks, but I just need the general idea of live-time chat message update. Thank you.
Upvotes: 0
Views: 658
Reputation: 102240
ActionCable is a Websockets framework for developing real time apps with Rails. Its developed by the Rails core team and rumor has it will be included in Rails 5.
You can give it a whirl already - but you should be prepared to do quite a bit of reading - websockets can be pretty daunting and requires a pretty good grasp of javascript and client / server interaction.
Upvotes: 1