I am currently trying to create a small chat website for a dedicated group of people.
The majority of the site is complete and working except from one major part which I continue to get completely lost within.
I need to send the data from the server ONLY when a piece of a database in MySQL has updated.
Firstly, I don't know how to have PHP check this database recursively every second or less. I have tried using AJAX but have noticed that this polls the server way too often and uses far too much bandwidth, I have also attempted to use Server-Sent Events with no avail due to it only sending data every 3 seconds which is FAR too slow.
In short; I need a type of push service which sends data TO the client periodically every 1 second or less and will only do this when it is needed.
while(1)
{
$result = mysqli_query($con,"SELECT Username, Message, DateT FROM Chat ORDER BY ID DESC LIMIT 30");
$list = array();
echo "data: ";
while($row = mysqli_fetch_array($result))
{
echo '<tr><td class="user">'.$row['Username'].'</td><td class="text">'.base64_decode($row['Message']).'</td><td class="time">'.$row['DateT'].' [GMT]</td></tr>';
}
echo PHP_EOL.PHP_EOL;
flush();
ob_flush();
sleep(1);
}