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

186
Vistas
The simplest way of websocket based on vanilla JS AJAX long polling

I prepare the simplest way of AJAX long polling (realtime) in vanilla JavaScript, without any libraries like jQuery... The code works based on timestamp of data.txt read by PHP file server.php (that's all).

function realtime(timestamp) {
    var xhr = new XMLHttpRequest();

    xhr.open('GET', 'server.php' + (typeof timestamp !== 'undefined' ? '?timestamp=' + timestamp : ''), true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var result = JSON.parse(xhr.responseText);

            document.getElementById('response').innerHTML = result.content;

            realtime(result.timestamp);
        }
    };
    xhr.send();
}

realtime();

Now, I would like to know how to prepare similar example in websocket (without any libraries, just clean JS/PHP).

It is possible?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Websocket is a different protocol than HTTP (it uses the HTTP upgrade mechanism). So you'll have to adapt your webserver AND any reverse proxy etc. to handle websocket connections.

over 4 years ago · Santiago Trujillo 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