1

I am running into problems trying to setup ActionCable with Milia. I was following Chris Oliver's ActionCable Group Chat segments and setting it up in my app using Milia. After a lot of searching on the Internet, my issue seems to be similar ones that arise with setting up ActionCable when you are using the Apartment gem for multi-tenancy as well. But, the related fixes I found for Apartment do not seem to apply to Milia.

I am currently getting the following error.

Could not execute command from ({"command"=>"subscribe", "identifier"=>"{\"channel\":\"ChatroomsChannel\"}"}) [NoMethodError - undefined method chatrooms' for nil:NilClass]: /home/ubuntu/workspace/rehabrx2/app/channels/chatrooms_channel.rb:4:insubscribed' | /usr/local/rvm/gems/ruby-2.4.0/gems/actioncable-5.1.4/lib/action_cable/channel/base.rb:177:in block in subscribe_to_channel' | /usr/local/rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:108:inblock in run_callbacks' | /usr/local/rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/execution_wrapper.rb:81:in wrap' | /usr/local/rvm/gems/ruby-2.4.0/gems/actioncable-5.1.4/lib/action_cable/engine.rb:66:inblock (3 levels) in '

Based on the solutions provided for the Apartment gem, the issue seems to be coming from the connection.rb and the chatrooms_channel.rb.

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

    def connected
      self.current_user = find_verified_user
      logger.add_tags "ActionCable", "User #{current_user.id}"
    end

    protected

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



class ChatroomsChannel < ApplicationCable::Channel

  def subscribed
    current_user.chatrooms.each do |chatroom|
      stream_from "chatrooms:#{chatroom.id}"
    end
  end

  def unsubscribed
    stop_all_streams
  end

  def send_message(data)
    Rails.logger.info data
    @chatroom = Chatroom.find(data["chatroom_id"])
    message   = @chatroom.messages.create(body: data["body"], user: current_user)
    MessageRelayJob.perform_later(message)
  end

end

I can't figure out how to set the tenant in ActionCable. I am hoping someone has already setup ActionCable with Milia. Can anyone give me some suggestions?

fool-dev
  • 7,671
  • 9
  • 40
  • 54

2 Answers2

2

You should use

indentifiers :current_user, :tenant

 def connect
  self.tenant = cookies[:tenant]
  Apartment::Tenant.switch!(tenant)
  self.current_user = find_verified_user
end

Actually I use The apartment gem

And inside yours channels in every method you first line should be

Apartment::Tenant.switch!(tenant) // variable tenant is the identifier defined in connection.rb 
Darlyn
  • 310
  • 1
  • 2
  • 16
0

Why not set your tenant whatever it is (at subsribe or whenever you have to do fetch data)

Tenant.set_current_tenant(tenant)

This uses current thread however I am not sure if it is 100% safe in actioncable environment

GorillaApe
  • 3,611
  • 10
  • 63
  • 106
  • I did see that in the Milia documentation and I did try to add it to the app but I am still getting the same error message. I assume I am not adding it properly and missing something. Do you have more detailed suggestions on what that might look like in the code? – Jonas Johnson Mar 26 '18 at 17:17
  • In your example I dont see it added. You could try at def subscribed, actually before any database operation – GorillaApe Mar 26 '18 at 18:51