Forum Moderators: open
if (typeof(EventSource) !== 'undefined') {
var source = new EventSource(membersSSE);
source.onmessage = function(e) {
mIconVal.html(e.data);
if (e.id == 'CLOSE') { source.close(); }
};
} header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo 'data: ';
$user ??= $_COOKIE['user'] ?? false;
if ($user) {
// do MySQL select query and print results
mysqli_free_result($mysql_results);
}
flush(); while (ob_get_level() > 0) { ob_end_flush(); }
flush();
if (connection_aborted()) break;
sleep(1); so I THINK that I should free it to ensure a fresh query result
Does this mean that it's running a MySQL query 10 times per second? If so, should I add a sleep(10); or something?
This is more of a PHP question than Javascript/AJAX
Is that script meant to run continuously (and missing those bits in your sample) or only once (which is what the above code implies)? In other words, is it actually streaming?
May I ask why you're streaming and not polling?
So yes, a bit of sleep might be good in that scenario.