I have an ActionCable method that subscribes the user. If a new convo is started, I want to subscribe the user to the new channel as well. I can't figure out the proper syntax for calling a channel method in a controller.
UPDATE: The issue is that the messages are appended to the chatbox when sent, but when the first message, is sent, the websocket connection is not established yet, and therefore it looks to the user as if the message was not sent (because it's not being appended).
channel/msgs_channel.rb
class MsgsChannel < ApplicationCable::Channel
#This function subscribes the user to all existing convos
def subscribed
@convos = Convo.where("sender_id = ? OR recipient_id = ?", current_user, current_user)
@convos.each do |convo|
stream_from "msg_channel_#{convo.id}"
end
end
#This is a new function I wrote to subscribe the user to a new convo that is started during their session.
def subscribe(convo_id)
stream_from "msg_channel_#{convo_id}"
end
end
In my convos controller, create method, I have tried several things:
convos_controller.rb
def create
@convo = Convo.create!({sender_id: @sender_id, recipient_id: @recipient_id})
ActionCable.server.subscribe(@convo.id)
end
ActionCable.subscribe(@convo.id)
error:
NoMethodError (undefined method
subscribe' for ActionCable:Module)`
ActionCable.msgs.subscribe(@convo.id)
error:
NoMethodError (undefined method
msgs' for ActionCable:Module):`
App.msgs.subscribe(@convo.id)
error:NameError (uninitialized constant ConvosController::App):
MsgsChannel.subscribe(@convo.id)
error:NoMethodError (undefined method
subscribe' for MsgsChannel:Class`
ActionCable.server.subscribe(@convo.id)
error:NoMethodError (undefined method
subscribe' for #):`