mercredi 8 mars 2017

How can I call a channel method in a rails controller?

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.

channel/msgs_channel.rb

class MsgsChannel < ApplicationCable::Channel  
  def subscribe(convo_id)
      stream_from "msg_channel_#{convo_id}"
  end
end

convos_controller.rb

def create
  @convo = Convo.create!({sender_id: @sender_id, recipient_id: @recipient_id})
  ActionCable.server.subscribe(@convo.id)
end

In my convos controller, create method, I have tried several things:


ActionCable.subscribe(@convo.id)

error: NoMethodError (undefined methodsubscribe' for ActionCable:Module)`


ActionCable.msgs.subscribe(@convo.id)

error: NoMethodError (undefined methodmsgs' for ActionCable:Module):`


  App.msgs.subscribe(@convo.id)

error:NameError (uninitialized constant ConvosController::App):


MsgsChannel.subscribe(@convo.id)

error:NoMethodError (undefined methodsubscribe' for MsgsChannel:Class`


ActionCable.server.subscribe(@convo.id)

error:NoMethodError (undefined methodsubscribe' for #):`

Aucun commentaire:

Enregistrer un commentaire