Scenario
To deliver instant notifications to the client, I'm sending an AJAX request to a PHP page that checks if there are new notifications: if there are, it outputs them, otherwise it sleeps for 10 seconds before trying again, so it has a basic structure similar to the following one.
<?php
$max_attempts = 10000;
for($i=0;$i<=$max_attempts;$i++){
check_new_notifications(); //checking notifications in an sql database
if(new_notification){
echo $notification;
break;
}
else{
sleep(10);
}
}
?>
What is missing
While this is great for the client, as it doesn't have to send a request every 10 seconds, but just one, I can't find a better way for the server than to check every 10 seconds if there are new notifications.
My question
What solution could I adopt in order to avoid the server to constantly check for new notifications?