anotherthink
anotherthink

Reputation: 491

ActionCable "subscribed" method not getting called

I have an ActionCable channel whose "subscribed" method never appears to get called.

In Rails logs and in Javascript console, I can see that the connection is being made:

I, [2017-07-14T16:02:28.759843 #10658]  INFO -- : [ActionCable]  Registered connection (Z2lkOi8vd2ViY2FzdC9Vc2VyLzM0NQ)
...
I, [2017-07-14T16:14:18.594274 #12208]  INFO -- : [2cdfb51d-e500-49bd-8d7d-c941b8b2bb8a] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)

The code in app/assets/javascripts/channel/chats.coffee (below) appears to be called:

 ... 
 App.global_chat = App.cable.subscriptions.create {
      channel: "ChatsChannel"
      chat_room_id: $("#chat_room_id").val()
      user_agent: navigator.userAgent
    },
 ...

as the browser console shows the chat object and it looks as expected:

> App.global_chat
< Subscription {consumer: Consumer, identifier: "{"channel":"ChatsChannel","chat_room_id":"32","use… like Gecko) Chrome/58.0.3029.110 Safari/537.36"}", connected: function, disconnected: function, received: function…}
...

But the log shows nothing from the ChatsChannel "subscribed" method (pasted below), and the "register_connection" method is never called.

# app/channels/chats_channel.rb
class ChatsChannel < ApplicationCable::Channel

  def subscribed
    logger.add_tags 'ActionCable', "subscribing"
    logger.info("subscribing")
    stream_from "chat_rooms_#{params['chat_room_id']}_channel"
    current_user.register_connection(params[:chat_room_id], params[:user_agent])
  end
  ...
end

Any thoughts? I'm quite new to ActionCable. All the documentation I can find says the "subscribed" method is just automatically called when the connection is registered, which it seems to be, so I can't figure out what's going wrong here.

This is actioncable 5.0.1.

Is there any way to force the ruby "subscribed" method via Javascript?

Upvotes: 1

Views: 1324

Answers (2)

HJW
HJW

Reputation: 402

Don't forget to adjust config/cable.yml to redis as well:

development:
  adapter: redis

and add it to your Gemfile:

gem 'redis'

Upvotes: 0

anotherthink
anotherthink

Reputation: 491

It turned out that the issue was that my redis server was not running in the background. Running redis-server outside of the Rails app solved this issue!

Upvotes: 1

Related Questions