Request polling

Racheal Mwatela
2 min readMar 12, 2021

In one of my recent tasks, I learnt about polling and how it’s used in front-end development.

So, what is it?

When building an application, we request the server to provide us with certain information. By the time you are making a request, the data is not ready or synced and the server lets you know about it. But how would you know the data is ready if you do make a request again? That is where polling comes in.

Polling is a way of requesting new data after a certain time interval from the API. In my research, it has been compared to Websockets but I won’t be diving much into how the two are different for today.

When do you use polling?

I used polling when I had to fetch data that was in a queue and had to keep checking if it’s available after every minute. Other examples include having real-time applications like an uber or google maps, where you have to keep updating directions on the map.

In this article, I am going to cover how to implement polling in Clojurescript. If you are using Clojurescript, using js setInterval or setTimeoutis the way to go.

This is how your code will most probably look like:

(ns exmaple.core) (js/setInterval (fn [] ;; add your request here) 6000)

6000 represents the timeout in milliseconds (the time we have to wait before the callback function is executed again)

With setInterval , the callback function will be called repeatedly at the specified time interval.

setTimeout will be implemented similarly to the above :

(ns exmaple.core)(js/setTimeout (fn [] ;; add your request here) 6000)

except this function runs only once after the time interval.

Each of these has it’s own use case and advantages. If you know you will receive your data after x amount of time, I would recommend using js/setTimeout to avoid making unnecessary requests but if you are uncertain of the data you expect or time, js/setInterval would do.

That is it! Now go yee and make those polling requests!

I have attached more resources on how to clear your polling ids as well as how to poll in Clojure.
1. Comprehensive way of polling with setInterval
2. Polling on Clojure

--

--

Racheal Mwatela

Hey, I am a Software Engineer with a passion for mentorship and career development