What is polling?
Polling is a process when a client (our browser in this case) continually sends requests to a server to check if updates are available. Polling is what is called pull technology. It’s often used to emulate push technology. Before we go on, let’s describe these terms:
Definition: Initial request for data originates from a client, and then is responded to.
Example: A web browser requests a web page.
Push technology (also known as push notifications)
Definition: A server publishes data, and a client that is subscribed is receiving data.
Example: A email server transmits an email to an email client.
So, instead of doing publish / subscribe mechanism, where the server sends data updates and we listen for those updates, we are going to repeatedly ask the server whether changes did actually happen.
How to do polling in Rails?
OK, now that we explained what polling is, let’s get down to business. In order to do polling in Rails you will need to write 2 things:
- Controller action that will be polled and the response/view it returns
First part is pretty straightforward. Let’s say we have a website that show’s movies and their ratings. Rating of a movie changes constantly, and we want to keep the user updated for any rating change that happens. For this case, we will create a rating action inside the Movies controller.
Then, we can create a partial for the movie rating like this, so we can show it nicely to the user:
Here we defined a checkRating (line 6) function that will get called every 2 seconds by the setInterval at line 14. On the line 7, we are telling it to fetch a rating of a movie by specifying its ID in the request URL.
And voilà 🎉, if you go to your movie page and open the Network tab in your browser, you should see that request towards rating action is dispatched every 2 seconds.
Hold your horses 🎠, we are getting there. You can do simple polling with the new version of the render_async gem! In order to do this, you’ll need to install the gem inside your Rails application and put this snippet in your application.html.erb file.
<%= content_for :render_async %>
We will keep movies_controller.rb and _movie_rating.html.erb files from the previous example, and we will only edit show.html.erb to look like this:
That’s not all you can do with it. You can throw in some error handling in there too, also without any JS code to be written:
Your reaction while reading this: WOW, I can get an error message to show up too 🤯? Where can I try out this gem?
Right here my friend. You’re welcome ❤️.
Polling feature was recently released in the 2.1.0 version of the gem and it’s ready to be used by the community. If you are using polling in your app, you can try out this feature of the gem.
You can also read the original blog post that made render_async famous and see his original use case on the Semaphore CI blog.
Also, retweeting this is an awesome way to help spread the word with your friends:
P.S. You can read this post on my blog — Pragmatic Pineapple 🍍