Ask A Question

Notifications

You’re not receiving notifications from this thread.

With Hotwire and a long-running background job, how can I show a message indicating that data is loading?

David asked in Rails

So I have a long-running API call running in a background job. As records are created/updated/removed in that job I have them broadcasted to the user. I would like to have a notification/badge added when the background job is started and have it removed when the job finishes (so the user doesn't think something is broken because not all their data is showing up).

I have experimented with a few ideas, but none of them worked or felt quite right (e.g. having a "loading" boolean field on the user and triggering broadcasts for when that specific field changed value).

Reply

Ultimately I setup the button-press that would queue the background job to send a turbo_stream response to append a loading partial

<%= turbo_stream.append "loading", partial: "shared/loading" %>

and at the end of the background job it broadcasts a removal:

# current_user is passed to the background job in part so we can broadcast the remove to the right user
Turbo::StreamsChannel.broadcast_remove_to("loading:#{user.id}", target: "loading_message")
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.