David

Joined

27,150 Experience
264 Lessons Completed
1 Question Solved

Activity

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")

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).

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.