Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app/assets/javascripts/channels/messages.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
App.messages = App.cable.subscriptions.create('MessagesChannel', {
$(document).ready(function() {
chatroomId = $('input#message_chatroom_id').val();
App.messages = App.cable.subscriptions.create({channel: 'MessagesChannel', chatroom_id: chatroomId}, {
received: function(data) {
$("#messages").removeClass('hidden')
$("#messages").removeClass('hidden');
return $('#messages').append(this.renderMessage(data));
},
chatroom_id: function(data) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we need this chatroom_id function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this code a long time ago but I believe it's needed to identify the chatroom.

return data.chatroom_id
},
renderMessage: function(data) {
return "<p> <b>" + data.user + ": </b>" + data.message + "</p>";
}
});
});
})
2 changes: 1 addition & 1 deletion app/channels/messages_channel.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MessagesChannel < ApplicationCable::Channel
def subscribed
stream_from 'messages'
stream_from "messages_#{params['chatroom_id']}_channel"
end
end
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def create
message = Message.new(message_params)
message.user = current_user
if message.save
ActionCable.server.broadcast 'messages',
ActionCable.server.broadcast "messages_#{message.chatroom_id}_channel",
message: message.content,
user: message.user.username
head :ok
Expand Down