ruby-on-railsruby-on-rails-6actioncable

Is it possible to use Action Cable in Rails 6 on Sprockets?


I realize this is a bit of a non-specific question, but I'm not exactly sure where my issue lies, so bear with me please.

I am attempting to setup Action Cable in Rails 6.1.4.1 on Sprockets (no webpacker integration). After setting up a basic channel, i am getting no result; and also no errors or anything to debug, so I really am not sure whre my issue lies.

Here's a look at the integration:

# config/cable.yml
development:
  adapter: async


# channels/notification_channel.rb
class NotificationChannel < ApplicationCable::Channel

  def subscribed
    stream_from "notification_channel"
  end


# app/javascript/channels/notification_channel.js
import consumer from "./consumer"

consumer.subscriptions.create("NotificationChannel", {
  connected() {
    console.log("Connected to notification channel...");
  },

  disconnected() {
  },

  received(data) {
  },

  notify: function() {
    return this.perform('notify');
  }
});


# app/javascript/channels/consumer.js
import { createConsumer } from "@rails/actioncable"
export default createConsumer()


# app/javascript/channels/index.js
const channels = require.context('.', true, /_channel\.js$/)
channels.keys().forEach(channels)

On server boot or page load, there is no indication in server logs of an active /cable channel, as well as no output in web console log. App.notification is undefined as well. And again, no errors client-side or server-side.

Does anyone know if this integration should be working normally with Sprockets? or is this just a configuration issue?


Solution

  • I was able to resolve this while remaining on Sprockets, like so:

    // assets/javascripts/cable.js
    //-------------
    //= require action_cable
    //= require_self
    //= require_tree ./channels
    
    // assets/javascripts/channels/consumer.js
    //--------------
    var consumer = ActionCable.createConsumer();