From f17fbaff49d84e6ed4e120df2d14a26b10001f45 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Fri, 7 Feb 2025 21:44:54 +1300 Subject: [PATCH] Add event-stream route --- server/app.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/server/app.js b/server/app.js index 4dec6b4c4..f586b760c 100644 --- a/server/app.js +++ b/server/app.js @@ -37,6 +37,8 @@ import bodyParser from 'body-parser'; import cookieParser from 'cookie-parser'; import forceSSL from './forcessl.mw.js'; +import EventEmitter from 'events'; + const sanitizeBrew = (brew, accessType)=>{ brew._id = undefined; @@ -352,7 +354,7 @@ app.get('/user/:username', async (req, res, next)=>{ app.put('/api/user/rename', async (req, res)=>{ const { username, newUsername } = req.body; const ownAccount = req.account && (req.account.username == newUsername); - + if(!username || !newUsername) return res.status(400).json({ error: 'Username and newUsername are required.' }); if(!ownAccount) @@ -512,6 +514,27 @@ app.get('/account', asyncHandler(async (req, res, 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 if(isLocalEnvironment){ // Login