Have problem with ActionCable. I send messages, but can see it only after I refresh page. In console I see, that ActionCable work good:
[ActionCable] [name10@nnnn.net] ChatsChannel is streaming from chats_1_channel
[ActionCable] [name10@nnnn.net] ChatsChannel#send_message({"message"=>"sdasda", "chat_id"=>1})
[ActionCable] [name10@nnnn.net] (0.2ms) BEGIN
[ActionCable] [name10@nnnn.net] Chat Load (0.3ms) SELECT "chats".* FROM "chats" WHERE "chats"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
[ActionCable] [name10@nnnn.net] SQL (1.6ms) INSERT INTO "messages" ("field", "user_id", "chat_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["field", "sdasda"], ["user_id", 1], ["chat_id", 1], ["created_at", 2016-07-27 20:45:45 UTC], ["updated_at", 2016-07-27 20:45:45 UTC]]
[ActionCable] [name10@nnnn.net] (17.0ms) COMMIT
[ActionCable] [name10@nnnn.net] [ActiveJob] Enqueued MessageBroadcastJob (Job ID: 6273cc4f-3a7a-4349-be41-195e893fe505) to Async(default) with arguments: #<GlobalID:0x007efb9417ea30 @uri=#<URI::GID gid://testchattask/Message/20>>
Message Load (1.3ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = $1 LIMIT $2 [["id", 20], ["LIMIT", 1]]
[ActiveJob] [MessageBroadcastJob] [6273cc4f-3a7a-4349-be41-195e893fe505] Performing MessageBroadcastJob from Async(default) with arguments: #<GlobalID:0x007efbc066c908 @uri=#<URI::GID gid://testchattask/Message/20>>
Here is chats_channel.rb:
class ChatsChannel < ApplicationCable::Channel
def subscribed
stream_from "chats_#{params['chat_id']}_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def send_message(data)
current_user.messages.create!(field: data['message'], chat_id: data['chat_id'])
end
end
chats.coffee:
jQuery(document).on 'turbolinks:load', ->
messages = $('#messages')
if $('#messages').length > 0
App.room = App.cable.subscriptions.create {
channel: "ChatsChannel"
chat_id: messages.data('chat-id')
},
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
messages.append data['message']
send_message: (message, chat_id) ->
@perform 'send_message', message: message, chat_id: chat_id
$('#new_message').submit (e) ->
$this = $(this)
textarea = $this.find('#message_field')
if $.trim(textarea.val()).length > 1
App.room.send_message textarea.val(), messages.data('chat-id')
textarea.val('')
e.preventDefault()
return false
cable.coffee:
@App ||= {}
App.cable = ActionCable.createConsumer()
I checked everything I can, but don't find problem.