ruby-on-rails-6actioncablestimulus-reflex

"The ActionCable connection is not open! `this.isActionCableConnectionOpen()` must return true before calling `this.stimulate()`"


I'am getting the above error while accessing stimulus reflex with rails 6.

  <a href="#"
  data-reflex="click->VotesReflex#increment"
  data-step="1" 
  data-count="<%= @count.to_i %>"
  >Increment <%= @count.to_i %></a>

 class VotesReflex < ApplicationReflex
  delegate :current_user, to: :connection
  def increment
    @count = element.dataset[:count].to_i + element.dataset[:step].to_i
  end
end

Solution

  • You should identify the connection with current_user to open the connection. Assuming you are using devise for authentication.

    Here is what you should do.

    module ApplicationCable
      class Connection < ActionCable::Connection::Base
        identified_by :current_user
    
        def connect
          self.current_user = find_verified_user
        end
    
        private
    
        def find_verified_user
          # rubocop:disable Lint/AssignmentInCondition
          if verified_user = User.find_by(id: cookies.signed['user.id'])
            verified_user
          else
            reject_unauthorized_connection
          end
        end
      end
    end
    

    In your initializers, create warden_cookies.rb

    Warden::Manager.after_set_user do |user, auth, opts|
      scope = opts[:scope]
      auth.cookies.signed["#{scope}.id"] = user.id
      auth.cookies.signed["#{scope}.expires_at"] = 30.minutes.from_now
    end
    
    Warden::Manager.before_logout do |_user, auth, opts|
      scope = opts[:scope]
      auth.cookies.signed["#{scope}.id"] = nil
      auth.cookies.signed["#{scope}.expires_at"] = nil
    end