Skip to content Skip to sidebar Skip to footer

Retrieving Data From A Database Using Ajax Without Requesting Every X Seconds

I have been reading up on how to use XMLHTTPRequests to grab data from a database via JavaScript. However, I have noticed that every single one of these 'tutorials' use an interva

Solution 1:

You can use EventSource to constantly stream data to browser.

const source = newEventSource("data.php");
// note, you can also set custom event handlers for named events
source.addEventListener("message", function(e) {
  // do stuff with `e.data`
});
// close event stream// source.close();

<?php 
 header("Content-Type: text/event-stream\n\n"); 
 header("Cache-Control: no-cache"); 
 // get, do stuf with `$data1`, `$data2`echo"data: $data1\n";
 echo"data: $data2\n\n";

Post a Comment for "Retrieving Data From A Database Using Ajax Without Requesting Every X Seconds"