Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda