-
Notifications
You must be signed in to change notification settings - Fork 22k
Description
Steps to reproduce
Create a new Rails 5 app and follow the basic guide for creating a chat application.
Expected behavior
Expecting Connect.rb to somehow pass the request to the proper channel file so it can be queried for a subdomain attribute and used with multi-tenancy
Actual behavior
When calling request
in the consults_channel.rb in the send_message
method I receive a
NameError - undefined local variable or method `request' for #<ConsultsChannel:0x007f8ff3343cc8>
System configuration
Rails version:
5.0.0.1
Ruby version:
2.3.1
####Disclaimer
I realize this should be asked on Stack or some other medium, but I'm trying to wrap my head around how connect.rb
can actually get the request but am unsure on how to pass it to the channel file. Here is my connect.rb and consults_channel.rb file for review.
Feel free to close this if there is not a wide use case or if this is documented elsewhere that I have overlooked.
connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
tenant = request.subdomain
Apartment::Tenant.switch!(tenant)
self.current_user = find_verified_user
logger.add_tags "ActionCable", "User #{current_user.id}", "Tenant #{tenant}"
end
protected
def find_verified_user
if current_user = env['warden'].user
current_user
else
reject_unauthorized_connection
end
end
end
end
consults_channel.rb (note I am hardcoding the Tenant switch from apartment for testing purposes until I can figure out how to get the request into the channel file.
class ConsultsChannel < ApplicationCable::Channel
def subscribed
current_user.consults.each do |consult|
stream_from "consults:#{consult.id}"
end
end
def unsubscribed
stop_all_streams
end
def send_message(data)
puts request
Apartment::Tenant.switch!('acme')
consult_message = current_user.consults.find(data["consult_id"]).consult_messages.create(body: data["body"], user: current_user)
ConsultMessageRelayJob.perform_later(consult_message)
end
end