Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionCable displays an empty page after action

I'm following Michael Hartl tutorial about Rails 5 ActionCable, to build a simple chat, and a problem is occurring inside my create action in MessageController:

  def create
    message = current_user.messages.build(message_params)
    if message.save
      ActionCable.server.broadcast 'room_channel',
                                     content:  message.content,
                                     username: message.user.username
      head :ok
    end
  end

It goes until head :ok, and after that, display an empty HTML page. The log is:

Processing by MessagesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"h9UsB78aX8mO8Nyn8eZEUuDzAOxL4V7UCMwNuTfwALliMv7OCkcUIeR4/xXIcvPjwq9H1bphjn6G96G+0VYisw==", "message"=>{"content"=>"oi"}, "commit"=>"Send"}
  User Load (1.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Message Load (2.1ms)  SELECT  "messages".* FROM "messages" ORDER BY "messages"."created_at" DESC LIMIT ?  [["LIMIT", 50]]
   (0.3ms)  begin transaction
  SQL (9.3ms)  INSERT INTO "messages" ("content", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["content", "oi"], ["user_id", 1], ["created_at", 2016-09-19 02:12:58 UTC], ["updated_at", 2016-09-19 02:12:58 UTC]]
   (9.1ms)  commit transaction
[ActionCable] Broadcasting to room_channel: {:content=>"oi", :username=>"alice"}
Completed 200 OK in 69ms (ActiveRecord: 22.0ms)


RoomChannel transmitting {"content"=>"oi", "username"=>"alice"} (via streamed from room_channel)
Finished "/cable/" [WebSocket] for 10.0.2.2 at 2016-09-19 02:12:58 +0000
RoomChannel stopped streaming from room_channel

(After that, I can refresh the page and the message is displayed properly)

Why that is happening?

My RoomChanel class:

class RoomChannel < ApplicationCable::Channel
  def subscribed
    stream_from "room_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

end

My room.coffee

App.room = App.cable.subscriptions.create "RoomChannel",
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) ->
  alert data.content
like image 991
rwehresmann Avatar asked Nov 09 '22 08:11

rwehresmann


1 Answers

I know the answer is accepted already, but this solution worked for me without having to remove head :ok, maybe it helps somebody out.

Normally in the development environment people are running localhost:3000 but i was running on a different port, localhost:3030.

And this case i had to add an allowed request origin for that port, like so:

config.action_cable.allowed_request_origins = [
      'http://localhost:3030'
  ]

to my /config/environments/development.rb

like image 104
luissimo Avatar answered Nov 15 '22 05:11

luissimo