JS PHP

AJAX LONG POLLING WITH PHP

(simple example)

var last = 0; function poll () {   SEND LAST UPDATED TIMESTAMP   let data = new FormData();   data.append("last", last);

LONG POLL AJAX FETCH

01

  AJAX FETCH & LOOP   fetch("SCRIPT.PHP",    {method:"POST", body:data})   .then(res => res.json()).then(data => {     console.log(data);     last = data.time;     poll();   }).catch(e => poll());  }

START poll();

SET TIME LIMIT set_time_limit(30); ignore_user_abort(false);

PHP UPDATE CHECK LOOP

02

RESPOND ONLY ON UPDATE while (true) {   $data = getData();   if ($data["time"] > $_POST["last"])     { echo json_encode($data); break; }   sleep(1); }