Hello guys,
I'm trying to implement Server Side Events (SSE) in APEX to built a small chat application. If it works it I could expand the functionality to actual work related systems.
The idea is to keep the connection open and receive the data to update the chat box as soon as something new comes up. So far so good.

But I've noticed that the network keeps opening and closing request, which defeats the purpose since I could do a simple setInterval in JS instead of the whole thing.
Currently I've tried the code below (with variations)
AJAX Callback application process
DECLARE
v_result clob := '[]';
BEGIN
owa_util.mime_header('text/event-stream');
-- while 1 = 1 loop
SELECT JSON_ARRAYAGG( /**** LOTS OPF COLUMNS ***/RETURNING CLOB )
into v_result
FROM /**LOTS_OF_TABLES**/
if v_result is null then
v_result := '[]';
end if;
:AI_LAST_UPDATE := to_char(sysdate, 'DD-MM-YYYY HH24:MI:SS');
htp.p('data:' || v_result);
htp.flush;
DBMS_SESSION.SLEEP(2);
-- end loop;
owa_util.http_header_close;
end;
JavaScript
let source = new EventSource("f?p=&APP_ID.:0:&APP_SESSION.:APPLICATION_PROCESS=Notifications:");
source.addEventListener("message", e => console.log('SSE Package:\n',e.data));
But by adding the loop in the PLSQL code it stops printing the responses.
If I add owa_util.http_header_close;
inside the loop I also don't get any response.
What is the code missing to be able to provide a stable open connection with the server?