0

I am working on Hartl's tutorial Learn Enough Action Cable, and I'm stuck on section 4.1. I copied and pasted the code from the tutorial just to be sure, and the pop up window with data.content still wont come up.

Messages Controller:

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

room_channel.rb:

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

    def unsubscribed
    end
 end

routes.rb:

Rails.application.routes.draw do
root 'messages#index'
resources :users
resources :messages
get    '/login',   to: 'sessions#new'
post   '/login',   to: 'sessions#create'
delete '/logout',  to: 'sessions#destroy'

mount ActionCable.server, at: '/cable'
end

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)

Every other part of the tutorial worked fine with pop-up windows coming up for javascript alerts. In my server logs it says:

Started GET "/cable" for 127.0.0.1 at 2017-03-26 13:59:12 -0400
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-03-26 13:59:12 -0400
Successfully upgraded to WebSocket (REQUEST_METHOD: GET,HTTP_CONNECTION:     keep-alive, Upgrade, HTTP_UPGRADE: websocket)
RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room_channel
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-03-26 14:06:26 -0400
RoomChannel stopped streaming from room_channel
Started GET "/messages" for 127.0.0.1 at 2017-03-26 14:06:26 -0400
Processing by MessagesController#index as HTML
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?   LIMIT ?  [["id", 1], ["LIMIT", 1]]
Message Load (0.2ms)  SELECT  "messages".* FROM "messages" ORDER BY        "messages"."created_at" DESC LIMIT ?  [["LIMIT", 50]]
 Rendering messages/index.html.erb within layouts/application
Rendered messages/_messages.html.erb (0.6ms)
Rendered messages/_message_form.html.erb (2.0ms)
Rendered messages/index.html.erb within layouts/application (4.5ms)
Rendered layouts/_logo.html.erb (0.4ms)
Rendered layouts/_header.html.erb (2.0ms)
Completed 200 OK in 71ms (Views: 36.4ms | ActiveRecord: 1.5ms)

1 Answers1

1

I have faced the same problem.

As you can also see that message is getting broadcated but not received at the client side i.e room.coffee

So the problem is that CoffeeScript does not have proper open and closing brackets. So it work in the idea of proper indentation.

When you are copying the code from web, the indentation is not proper.

In following code make sure that you have only 2 spaces after received: data like this:

received: (data) -> alert(data.content)

alert statement should have two space gap from the begining.

Moreover your function like connected and received should also be 2 spaces inside with respect to App.room = App.cable.subscriptions.create "RoomChannel", line

Finally overall your code should look like this:

App.room = App.cable.subscriptions.create "RoomChannel",
  connected: ->

  disconnected: ->

  received: (data) ->
    alert(data.content)

Make sure you use spaces not TABs

ritzz.soni
  • 335
  • 1
  • 15