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:in
subscribed' | /usr/local/rvm/gems/ruby-2.4.0/gems/actioncable-5.1.4/lib/action_cable/channel/base.rb:177:inblock in subscribe_to_channel' | /usr/local/rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:108:in
block in run_callbacks' | /usr/local/rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/execution_wrapper.rb:81:inwrap' | /usr/local/rvm/gems/ruby-2.4.0/gems/actioncable-5.1.4/lib/action_cable/engine.rb:66:in
block (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?