I'm using Rails 5 in a docker environment and I can get Action Cable to broadcast on a Sidekiq worker perfectly fine, using worker.new.perform.
But for the life of me, I cannot get it to broadcast while using worker.perform_async.
Here is my cable.yml:
default: &default
adapter: redis
url: <%= ENV['REDIS_PROVIDER'] %>
development:
<<: *default
test:
<<: *default
production:
<<: *default
Here is my worker:
class AdminWorker
include Sidekiq::Worker
def perform
ActionCable.server.broadcast 'admin_channel', content: 'hello'
end
end
My Admin Channel:
class AdminChannel < ApplicationCable::Channel
def subscribed
stream_from "admin_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
As I mentioned earlier, this works just fine when calling AdminWorker.new.perform. As soon as I try to run AdminWorker.perform_async, the cable will not broadcast and nothing helpful regarding action cable in the logs. What am I missing here?
I had the same problem. Came across this answer: ActionCable.server.broadcast from the console - worked for me. Basically just changed cable.yml
development:
adapter: async
to
development:
adapter: redis
url: redis://localhost:6379/1
and I was able to broadcast from console, models, Sidekiq worker classes, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With