keerthana
keerthana

Reputation: 93

How to display flash object after sidekiq worker finishes-rails

I am trying to display reminder notification as a flash message in my rails app using sidekiq. I learnt how to set the flash message in the worker from this answer.

As mentioned in the last point of that answer, how to show the notification automatically, when an entry is made?

Also how to delete that entry once the flash message is closed?

I'm a beginner. Kindly help.

As requested, I'm adding my code attempt:

In my controller:

def show
  @user = User.find(params[:id])
  msg = $redis.get("messages:#{@user.id}")
  flash[:info] = msg if !msg.nil?
end

In my sidekiq worker:

def perform user, type
  @user = user`enter code here`
  message = "Message content"
  $redis.set("messages:#{user.id}", message)
end

What I want is to refresh user page when the redis key is set and also delete the pair when the close button of the flash is pressed.

Upvotes: 0

Views: 1239

Answers (1)

Mike Perham
Mike Perham

Reputation: 22238

Sidekiq is designed for asynchronous job processing. Once you've created a job, your controller is done and renders the page for the browser. Your job might be processed 1ms from now or 1 hour from now, you don't know. In other words, you can't do what you want. You should design your page flow to either account for this or don't create a background job for the work.

Upvotes: 2

Related Questions