Add event-stream route

This commit is contained in:
G.Ambatte
2025-02-07 21:44:54 +13:00
parent 10fae6dbac
commit f17fbaff49
+23
View File
@@ -37,6 +37,8 @@ import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser'; import cookieParser from 'cookie-parser';
import forceSSL from './forcessl.mw.js'; import forceSSL from './forcessl.mw.js';
import EventEmitter from 'events';
const sanitizeBrew = (brew, accessType)=>{ const sanitizeBrew = (brew, accessType)=>{
brew._id = undefined; brew._id = undefined;
@@ -512,6 +514,27 @@ app.get('/account', asyncHandler(async (req, res, next)=>{
return next(); return next();
})); }));
// Event Stream
const Stream = new EventEmitter;
// After Stream starts, send initStream event
setTimeout(()=>{
Stream.emit('push', 'initStream', { time: (new Date).toString() });
}, 1000);
app.get('/stream', (req, res)=>{
res.writeHead(200, {
'Content-Type' : 'text/event-stream',
'Cache-Control' : 'no-cache',
'Connection' : 'keep-alive'
});
Stream.on('push', (event, data)=>{
// console.log('Event:', event, 'Data:', data);
res.write(`data: ${JSON.stringify({ ...data, eventType: event })}\n\n`);
});
});
// Local only // Local only
if(isLocalEnvironment){ if(isLocalEnvironment){
// Login // Login