ActionCable not calling #subscribe in channel only Registering connection

Viewed 1357

I am running simple Rails app with Smart Admin theme. I tried a lot but cannot run Action cable on my application.

my_own_channel.rb

class MyOwnChannel < ApplicationCable::Channel
  def subscribed
    stream_for 'my_own_channel'
  end
  def unsubscribed; end
end

channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.email
    end

    protected

    def find_verified_user
      if verified_user = env['warden'].user
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

../app/assets/javascripts/channels/my_own.js

App.status_monitor = App.cable.subscriptions.create("MyOwnChannel", {
  received: function(data) {
    alert('Received....');
  },
  connected: function() {
    alert('connected');
  },
  disconnected: function() {
    alert('disconnected now');
  }
});

../app/assets/javascripts/cable.js

//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);

After doing all this, In logs I do not see the stream. In logs I see :

Started GET "/cable" for 127.0.0.1 at 2017-05-19 22:20:36 +0630
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-05-19 22:20:36 +0630
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (1.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
[ActionCable] [abcdefghijklmn@gmail.com] Registered connection (abcdefghijklmnopqrstuvwxyz)

And not streams connected are there. The other issue is, I see the connect method in connection.rb being called but the subscribe method in the MyOwnChannel is not being called. I have no clue what is missing, I also mounted the /cable routes in the routes.rb file.

2 Answers

This was also an issue for me. Removing pace.js will fix it, but you can also disable tracking of websockets which should also do the trick:

// set this before you load pace.js
window.paceOptions = {
  ajax: {
    trackWebSockets: false
  }
}

(See source here)

There's also an open issue on the pace project: https://github.com/HubSpot/pace/issues/411

Related