Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
how to refresh html page to update every time websocket sends data

I want to update a simple html frontend with data coming in sporadically from the server via a websocket (I'm using socket.io). For each data, I want to create a <p> element with text inside it about the data, and append it as a child element inside <div>. Then I want the browser to automatically refresh the page to show it.

The backend is sending the data as it should when I logged it to the console. However, my frontend isn't rendering correctly. After data is sent, the browser doesn't refresh, thus not showing the <p> element. However, when I manually refresh it, the first <p> element would show and blink really fast. It does not show additional <p> elements as data is coming in.

Here is the frontend code in question:

    <div id="names"></div>

    <script src="/socket.io/socket.io.js"></script>
    <script>
      let socket = io();
      let nameDiv = document.getElementById('names');

      socket.on('message', data => {
        let newName = document.createElement('p');
        newName.textContent = data.name;
        nameDiv.appendChild(newName);
        location.reload();
      });
    </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

remove location.reload() from your code, then try again. You don't need to "refresh" in order for the DOM to reflect the changes you have made, the event loop will render the changes immediately.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!