ruby-on-railssidekiqactioncable

Getting Action Cable to broadcast in Sidekiq Job


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?


Solution

  • 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.