Keeping API Data Up to date

If I have a RESTFUL API, and a front end with it. I am making requests to that API when the site loads.

Now how would I keep the data up to date. Like, if the user keeps the site open for 1 hour, but the data might have changed a bit.

So what would be the best way to ensure the data is up to date?

Hello!

Perform requests every N minutes:

setInterval(() => {
  fetch(yourApiUrl).then((data) => {
  });
}, 60*5*1000); // Request every 5 minutes (60sec * 5min * 1000 milis)

You can also implement some kind of push notifications from the server (if you have access to it).

1 Like