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.