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