Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 525
ChrisPalmer Avatar asked Feb 21 '17 20:02

ChrisPalmer


1 Answers

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.

like image 147
Navin Gupta Avatar answered Sep 25 '22 21:09

Navin Gupta