0

I am still in early stages of my programming and I was thinking about creating something which does API calls after every x seconds and update my website with new content.

My intial goal is to populate a table with the content obtained from API using FOR loop (.ejs page).

Now, I want to update just those rows and columns (created from FOR loop) of my webpage after x seconds instead of refreshing my entire page. How can I achieve this (Updating those rows and columns) ?

Consider stock market website, where it just updates the stock price, instead of entire page.

Thanks for your help in advance

8
  • you didn't ask for any help. What is your question? Commented Mar 2, 2018 at 20:47
  • @RandyCasburn Just updated my question. Commented Mar 2, 2018 at 20:49
  • investigate the use of Socket.io and websockets. With web socks you don't need to set a timer, you create a connection with the server and the server (your Nodejs) can push updates. Updating content is simple DOM stuff. Commented Mar 2, 2018 at 20:59
  • @RandyCasburn You still need to set a timer if you're using sockets. It just moves from being a timer you set at the client, to being one that you set at the server. Commented Mar 2, 2018 at 21:15
  • @Dan Prince - ohhhh nooo. The entire concept changes in a very good way. Can now focus on data flow and not arbitrary timers. Send when data needs to be sent rather than at an arbitrary time at which data may be exactly the same Commented Mar 2, 2018 at 21:18

2 Answers 2

2

The most popular way to solve this problem is to store the data you obtain from the other API in your database, then create an endpoint that serves the most recent version of that data as JSON.

Then at the client side, you make periodic requests to the server to fetch the content and updates a part of the page to show the newest data available.

This could be as simple as:

// server side (if you use express)

app.get("/data", (req, res) => {
  database.getMostRecentEntry()
    .then(data => res.json(data))
    .catch(err => res.status(500).end());
});


// client side

function fetchMostRecentData() {
  fetch("/data")
    .then(response => response.json())
    .then(data => updateView(data))
    .catch(err => showError(err));
}

function updateView(data) {
  let container = document.getElementById("app");

  container.innerHTML = `
    <tr>
      <td>${data.name}</td>
      <td>${data.value}</td>
    </tr>
  `;
}

function showError(err) {
  console.error(err);
  alert("Something went wrong");
}

// call fetchMostRecentData once every 10s
setInterval(fetchMostRecentData, 10000);

Now, this isn't a very robust solution and there are some fairly serious security problems, but it's a starting point.

From here, you should look into using a frontend framework (rather than updating innerHTML yourself). You could also look at using websockets, rather than serving the data through a HTTP endpoint.

Sign up to request clarification or add additional context in comments.

4 Comments

Don't need AJAX here. WebSockets and allow the server to push.
@RandyCasburn For someone who's still learning to program, sockets are overkill. They're harder to debug, harder to scale, and harder to setup.
Chicken! JK - I prefer to not downplay a person's aptitude based upon other's inability.
@RandyCasburn No one's aptitude is being downplayed. Learning is an incremental process—which is why I mentioned sockets at the end of my answer. I just prefer not to intimidate new programmers.
1

I'd look into using expressjs in combination with node.js to build your website. Then using ajax inside your html to accomplish the rest api call updates.

3 Comments

Don't need AJAX here. WebSockets and allow the server to push.
AJAX is much simpler for a beginner imo
Chicken! JK - I prefer to not downplay a person's aptitude based upon other's inability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.