How to periodically fetch/update value in rails? How to periodically fetch/update value in rails? ajax ajax

How to periodically fetch/update value in rails?


I would go with Firebase as opposed to polling the server.

However, if you're wanting to poll the server periodically, I would just have a JavaScript method which executes every 10 seconds, or at whatever interval you'd like, and fetches whatever data you'd like asynchronously and subsequently updates the DOM.

Also, ruby wrapper for firebase api, in case that may be of interest


I would say the easiest approach doing it would be using ActionController::Live. Using it you'll be able to send SSE(Server Sent Event) to your client while having js script there to catch and update your <%= @post.value %>. Here's a pretty nice manual on using it.

Still from my point of view the most appropriate way to implement things you want to do will be using something like Faye. It is a publish/subscribe messaging system which will allow you to update your client with new data as soon as it appears, e.g., you can set an after_save in your model and publish an update to all subscribed clients. And of course there is a gem also called faye to wrap its usage.