mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-09-22 12:02:57 +00:00
Add event-stream route
This commit is contained in:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user