Search code examples
ruby-on-railswebsocketreal-timepumaactioncable

ActionCable never establishes connection to channel


I am trying to get a first ActionCable example running, however, I feel like there is never a connection from client to channel established. I have pretty much followed the edge guides on everything else not mentioned here.

# app/channels/notifications_channel.rb
class NotificationsChannel < ApplicationCable::Channel
  def subscribed
    byebug
    stream_from "notifications"
  end

  def unsubscribed
    stop_all_streams
  end

  def receive(data)
    ActionCable.server.broadcast("notifications", data)
  end
end

byebug never has been called. I actually can put in a bunch of syntax gibberish into that file and will not see an error anywhere. It's like the file is never loaded.

Here is the javascript though, and I would expect to see a "connected" alert if it would establish a connection, which never happens. The file is loaded and executed properly though, as I can inspect the App variable in my browser console and it will show me the App.notifications as a valid Subscription object (it doesn't help much debugging though as most properties are functions).

// app/assets/javascripts/channels/notifications.js
App.notifications = App.cable.subscriptions.create("NotificationsChannel", {
  connected: function() {
    // Called when the subscription is ready for use on the server
    alert('connected');
  },

  disconnected: function() {
    // Called when the subscription has been terminated by the server
  },

  received: function(data) {
    alert(data)
    // Called when there's incoming data on the websocket for this channel
    $("#notifications").prepend(data.html);
  }
});

If it helps any further, my Puma always starts in "Single mode", where I am not sure if this might be the issue nor how to fix it.


Solution

  • So here is the reason. I somehow just tried to set everything to default and see if it's gonna work, and it did. I then by slowly adding configs and code back figured out what actually was the issue, which was the following line in the config/environments/development.rb:

    config.reload_classes_only_on_change = false
    

    Setting this to true solved it.