0
votes

I tried to setup a basic notification worker to notify some user. But in my example my user didn't get any notifcation.

my controller action where I tried to perform my worker:

  def index
    NotificationWorker.delay().perform(current_user.id, 'error', 'refresh', 'test', 'asd')
    # ActionCable.server.broadcast "web_notifications_#{current_user.id}", { type: 'error', icon: 'refresh', title: 'title', body: 'body' }
  end

my notification_worker.rb

class NotificationWorker
  include Sidekiq::Worker

  def perform(user_id, type, icon, title, body)
    Notification.create(user_id, type, icon, title, body)
  end
end

and my notification model:

class Notification < ActiveRecord::Base
  # belongs_to
  belongs_to :users

  def self.create(user_id, type, icon, title, body)
    ActionCable.server.broadcast "web_notifications_#{user_id}", { type: 'error', icon: 'refresh', title: 'title', body: 'body' }
  end
end

when I use this

Notification.create(current_user.id, 'error', 'refresh', 'test', 'asd')

my user get the notification but I wan't to send them with sidekiq

1
do you have sidekiq running? - Andrey Deineko
nope, that was the Problem :( - Evolutio
Don't do NotificationWorker.delay().perform, do NotificationWorker.perform_async - Mike Perham
@MikePerham why I should use NotificationWorker.perform_async and not NotificationWorker.delay().perform? - Evolutio
Because that's not delays intended usage. Please read the docs. github.com/mperham/sidekiq/wiki/The-Basics#client Delay is for calling class methods on classes. github.com/mperham/sidekiq/wiki/… - Mike Perham

1 Answers

1
votes

As per my assumption in comments - the issue was that you did not run the sidekiq.