Mateusz Urbański
Mateusz Urbański

Reputation: 7862

TypeError: App.cable is undefined

I have a problem with running ActionCable on my machine.

In assets/javascripts/channels I have two files:

index.coffee

#= require action_cable
#= require_self
#= require_tree .

@App = {}
App.cable = ActionCable.createConsumer()

and question.coffee

App.question = App.cable.subscriptions.create "QuestionChannel",
  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) ->
    # Called when there's incoming data on the websocket for this channel

  follow: ->
    @perform 'follow'

  unfollow: ->
    @perform 'unfollow'

my application.js file looks like this:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require icheck
//= require homer/scripts
//= require Chart
//= require excanvas
//= require channels
//= require_tree .

When I start cable server and rails server and visit localhost:3000 in firefox console I see two errors:

SyntaxError: An invalid or illegal string was specified


this.webSocket = new WebSocket(this.consumer.url);

and

TypeError: App.cable is undefined


App.question = App.cable.subscriptions.create("QuestionChannel", {

I'm using rails 5 beta 3. How can I fix this?

Upvotes: 2

Views: 1663

Answers (3)

bright
bright

Reputation: 191

I had the same issue. I changed my server to puma whaaala it worked!

You can change by adding gem 'puma' and run bundle.

Also, make sure you config.action_cable.url = "ws://localhost:3000/cable" in your development.rb file. Good luck

Upvotes: 0

Prathamesh Sonpatki
Prathamesh Sonpatki

Reputation: 906

Make sure that you have

<%= action_cable_meta_tag %>

somewhere in your applicaiton layout.

Rails by default generates and it takes care of providing the cable URL to Action Cable so that you don't have to pass it to

ActionCable.createConsumer()

.

Upvotes: 1

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

ActionCable.createConsumer needs one parameter - address to Action Cable server.

@App = {}
App.cable = ActionCable.createConsumer("ws://cable.example.com")

If You miss this parameter, then this.customer.url is undefined.

Upvotes: 1

Related Questions