How do i use server sent event only when a new row is inserted in my table using php

Till now i have made this code, but this refreshes everytime and id again becomes 0. I want the serversent event to notify only when a new row is inserted in my table.

<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
require_once 'config.php';
$id=0;
echo "retry: 100n";
$sql = "select id from realtime order by id desc limit 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if($id!=$row['id'])
        {
        $id=$row['id'];
        echo "data: ".$id."nn";
        }
    }
}
else{
        echo "data: wrong isnn";
}
flush();
?>


Source: stackoverflow-php