I have a rails 4 application, that is doing a background calculus. So the user press a button and it launches a background job for the long calculus (using delay_job) and arrive on a waiting page. Despite the fact that the calculus is done asynchronously, I would like to find a way to warn the user and automatically reload it page once the calculus is finish.
I have looked at several solutions :
If you have any smart solution for this problem, or any advise for my limitation in the listed ideas.
I'll explain how this works for you
HTTP
When you send the request to Rails, your browser doesn't have any way to "listen" to what happens to anything other than a direct response. The HTTP request is "sent" & doesn't have any mechanism to wait until the asynchronous process is complete
Your job is to give your app a "listener" so that your front-end can receive updates as they are generated (out of scope of HTTP), hence the websocket or SSE stuff
We've set up what you're looking for before, using Pusher
"Live"
Achieving "live" functionality is what you need
This basically means keeping a request open "perpetually" to the server; listening to any of the "events" the server sends back. This is done with JS on the front-end, where the client will "subscribe" to a channel (typically a user-centric one), and will then have all the data sent to it
We used a third-party system called Pusher
to achieve this recently:
#GemFile
gem "pusher", "~> 0.12.0"
#app/controllers/message.rb
def send_message
public_key = self.user.public_key
Pusher['private-user-' + public_key].trigger('message_sent', {
message: "Message Sent"
})
end
#app/assets/javascripts/application.js
pusher = new Pusher("***********",
cluster: 'eu'
)
channel = pusher.subscribe("private-user-#{gon.user}")
channel.bind "message_sent", (data) ->
alert data.message
Hope this gives another option for you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With