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

180
Views
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?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

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