Is it possible to implement SSE on Joomla, like how it is implemented in pure PHP here?
Here is what I have so far:
administrator/components/com_component/controllers/sync.php
class ComponentControllerSync extends JControllerLegacy
public function execute()
{
$close = array(
'id' => 'CLOSE',
'message' => 'stream closed');
$progress = array(
'id' => 'PROGRESS',
'message' => '100/100 done',
'progress' => '100%');
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo "data: " . json_encode($progress) . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
sleep(5);
echo "data: " . json_encode($close) . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
JFactory::getApplication()->close();
}
}
JavaScript which calls the controller
(function($, window, document){
$(function(){
$('#sync').on('click', function(){
$.eventsource({
label: 'evt',
url: 'index.php?option=com_component&task=sync.execute',
dataType: 'text',
open: function(data) {
console.log('started');
},
message: function(e) {
var json = e;
var id = e.id;
if(id == 'PROGRESS'){
console.log(json.message + json.progress);
}
else if(id == 'CLOSE'){
console.log(json.message);
$.eventsource('close', 'evt');
}
else if(id == 'ERROR'){
console.log(json.message);
$.eventsource('close', 'evt');
}
}
});
});
});
})(window.jQuery, window, document);
Joomla takes all of the echos into a buffer then presents the output to the client, which is the opposite of what I wanted. Is there a way to go around this buffer and stream the echos?