Question

Trying to use Sidekiq to send push events to the client. Here's my code:

clock.rb

every(3.minutes, 'update_events') { PushEventWorker.perform_async('events_updated') }

push_event_worker.rb

class PushEventWorker

  include Sidekiq::Worker
  sidekiq_options :retry => false, :timeout => 2.minutes

  def perform(job_name)
    job_name = job_name.to_sym

    if :events_updated
      Pusher['events'].trigger('events_updated', {})
  end

end
application.html.erb
<script src="http://js.pusher.com/2.1/pusher.min.js" type="text/javascript"></script>
<script type="text/javascript">

  Pusher.log = function(message) {
    if (window.console && window.console.log) {
      window.console.log(message);
    }
  };

  var pusher = new Pusher('<%= ENV['pusher_key'] %>');

  var eventsChannel = pusher.subscribe('events');
  eventsChannel.bind('events_updated', function() {
    App.Pollers.Event.poll();
  });

</script>

I get the following output in the log:

Pusher : Event recd : {"event":"pusher:pong","data":{}}

However, when I call PushEventWorker.new.perform('events_updated'), it's a success. Why does this work but the above doesn't?

Was it helpful?

Solution

In your push_event_worker.rb, perform function should look like this:

def perform(job_name)
  job_name = job_name.to_sym

  if jobname == :events_updated
    Pusher['events'].trigger('events_updated', {})

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top