I have a php server file and an HTML client file, the HTML file send ajax requests to the server to retrieve data every 500 ms
, this although works as expected it's causing high usage of memory and CPU on the client's device.
PHP
if(isset($_POST['id']) && $_POST['id'] != '' )
{
$id = $_POST['id'];
$select = $con->prepare("SELECT * FROM data WHERE id=?");
$select->bind_param('s', $id);
$select->execute();
$result = $select->get_result();
while($row = $result->fetch_assoc())
{
echo $row['column 1'] . "\t" . $row['column 2'] . "\n";
}
}
AJAX
function send(){
var formdata = new FormData(),
id = document.getElementById('id').value;
formdata.append('id', id);
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('post', 'server.php', true);
xhr.send(formdata);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
console.log(xhr.responseText);
}
}
}
setInterval(function(){send()}, 500);
I would like to find an alternative solution to ajax, instead of sending numerous requests to the server and retrieving same data most of the time, it would be much more efficient if the server can interact with the client on data change or update.
I can't use PHP Socket
or HttpRequest
methods as they are not installed on my hosting server and I'm not sure if the later works. The only way I can think of is using SESSIONS
.
According to this PHP server store all users sessions on the same directory on the server, therefore it may be possible to change sessions variables for a particular user directly on the file. The problem however is the data in those files are serialized and I'm not sure how to de-serialize the data and re-serialize them and then save the new data!
Even if I was able to find a way to store updates on the session file, I still need to use setInterval to listen to the session's variable change every 500ms
although it's not ideal but it would be much better than using XMLHttpRequest
in terms of memory and CPU usage.
So what's the best way to do this? any help would be much appreciated.
UPDATE:
I realized that SESSION
wont work because it can be read only by the server not the client, therefore i have to send ajax request to the server to get the variables which i was trying to avoid.
I tried long polling but i had many problems with it, flush
and ob_flush()
doesn't work on my server and i can't change the ini
settings. When trying the infinite loop i can't get it to break on data change:
if(isset($_GET['size']) && $_GET['size'] != '')
{
$size = (int)$_GET['size'];
$txt = "logs/logs.txt";
$newsize = (int)filesize($txt);
while(true) {
if($newsize !== $size) {
$data = array( "size" => filesize($txt), "content" => file_get_contents($txt));
echo json_encode($data);
break;
}
else{
$newsize = (int)filesize($txt);
usleep(400000);
}
}
}
it keeps going on and on, even if the logs.txt
size increase it won't break! how can I make it break and echo data on size increase?
UPDATE 2:
It turned out the php cache the filesize when calling filesize()
method therefore the above loop will run indefinitely, the solution for that is to use clearstatcache()
method which will clear the stored cache of the file size allowing the loop to break on filesize changes.