1

I was trying out the action cable functionality of Rails 5. If I try to broadcast using rake task, I am not receiving a message. Does anyone see the problem? The connection is being established. Thank you very much.

channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base


    def connect
      self.current_user = find_verified_user
    end

    private
    def find_verified_user
      if current_user
         return current_user
      else
        reject_unauthorized_connection
      end
    end

  end
end

FileStatusChannel.rb

class FileStatusChannel < ApplicationCable::Channel
  def subscribed
    matching_job = MatchingJob.find_by_uuid(params[:file_uuid])
    stream_for matching_job
  end
end

Javascript

<script>
        // Action Cable
        (function() {
            this.App || (this.App = {});

            App.cable = ActionCable.createConsumer();
        }).call(this);

        App.cable.subscriptions.create({
            channel: "FileStatusChannel",
            file_uuid: "18e01f4e-1d20-4905-b713-9ddc7adc4ae0",
            received: function(data) {
                console.log(data);
            }
        });
</script>

Rake Task

matching_job = MatchingJob.find_by_uuid("18e01f4e-1d20-4905-b713-9ddc7adc4ae0")
FileStatusChannel.broadcast_to(matching_job, {hello: "test"})

Rails Log

FileStatusChannel is transmitting the subscription confirmation
FileStatusChannel is streaming from file_status:Z2lkOi8vcmV0YWlscXVhbnQtc3RhYmxlL01hdGNoaW5nSm9iLzc
zer02
  • 3,963
  • 4
  • 31
  • 66
  • There is a bug in this code - `find_verified_user` does not actually return the value of `current_user`. Rather if `current_user` evaluates to true it will return nil. – max Sep 27 '17 at 12:06
  • @max I am using devise with a helper function in my `application_helper.rb` ` def current_user return unless session[:user_id] @current_user ||= User.find(session[:user_id]) end` – zer02 Sep 27 '17 at 12:09
  • Yes, but the logic in your private method is still broken. Try `if true; else; false; end` - it returns nil. – max Sep 27 '17 at 12:10
  • 1
    You need to write it as `if current_user; current_user; else; reject_unauthorized_connection; end`. – max Sep 27 '17 at 12:13
  • Yes, thanks. Even if I uncomment the auth process. The rake task does not send the message. – zer02 Sep 27 '17 at 12:14
  • Did you try checking the rails server log? – max Sep 27 '17 at 12:15
  • Yes. It only says the user subscribed to the FileStatusChannel with uuid. I also see the pings in the console of Chrome. But no broadcast is being emitted. – zer02 Sep 27 '17 at 12:17
  • You have to use middleware to transmit messages outside of the current rails process. Do you have Redis up and running? http://guides.rubyonrails.org/action_cable_overview.html#subscription-adapter – Derek Wright Sep 27 '17 at 14:34
  • @ddubs No, I wanted to avoid redis in development mode. Is it mandatory? – zer02 Sep 27 '17 at 14:35
  • Unfortunately it is mandatory since rake is starting a new process. Same thing if you tried to broadcast from the console as well. – Derek Wright Sep 27 '17 at 14:36
  • Possible duplicate of [ActionCable.server.broadcast from the console](https://stackoverflow.com/questions/35176934/actioncable-server-broadcast-from-the-console) – Derek Wright Sep 27 '17 at 14:40

0 Answers0