如何使用 Milia 在 ActionCable 中设置租户以实现多租户 [英] How to Set Tenant in ActionCable with Milia for Multi-Tenancy
问题描述
我在尝试使用 Milia 设置 ActionCable 时遇到问题.我正在关注 Chris Oliver 的 ActionCable 群聊片段,并使用 Milia 在我的应用程序中进行设置.在 Internet 上进行了大量搜索之后,我的问题似乎与在将 Apartment gem 用于多租户时设置 ActionCable 时出现的问题类似.但是,我为 Apartment 找到的相关修复似乎不适用于 Milia.
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.
我目前收到以下错误.
无法执行来自 ({"command"=>"subscribe", "identifier"=>"{\"channel\":\"ChatroomsChannel\"}"}) [NoMethodError - 未定义的方法 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: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: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:in wrap' |/usr/local/rvm/gems/ruby-2.4.0/gems/actioncable-5.1.4/lib/action_cable/engine.rb:66:in
块(3级)在'
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 '
根据为 Apartment gem 提供的解决方案,问题似乎来自 connection.rb 和 chatrooms_channel.rb.
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
我不知道如何在 ActionCable 中设置租户.我希望有人已经用 Milia 设置了 ActionCable.谁能给我一些建议?
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?
推荐答案
你应该使用
indentifiers :current_user, :tenant
def connect
self.tenant = cookies[:tenant]
Apartment::Tenant.switch!(tenant)
self.current_user = find_verified_user
end
其实我用的是The apartment gem
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
这篇关于如何使用 Milia 在 ActionCable 中设置租户以实现多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!