u
This commit is contained in:
@@ -27,7 +27,7 @@ export function register(app, basePath = '') {
|
||||
* es.addEventListener('article-42000045', () => { refetchArticle('42000045'); });
|
||||
* ```
|
||||
*
|
||||
* Keep-alive comments (`: keepalive`) are sent roughly every 25s.
|
||||
* Keep-alive comments (`: heartbeat`) are sent roughly every 20s.
|
||||
* Reconnect after changing the selected company (the stream is bound
|
||||
* to the company at connect time).
|
||||
*
|
||||
@@ -76,24 +76,28 @@ export function register(app, basePath = '') {
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/events`, (req, res) => {
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return res.status(400).json({ error: 'No company selected' });
|
||||
}
|
||||
|
||||
req.socket.setTimeout(0);
|
||||
res.setTimeout(0);
|
||||
|
||||
res.status(200);
|
||||
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
||||
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
res.setHeader('X-Accel-Buffering', 'no');
|
||||
res.flushHeaders?.();
|
||||
|
||||
res.write(': connected\n\n');
|
||||
|
||||
subscribe(res, { companyId, userId: req.user.id });
|
||||
});
|
||||
app.get(`${basePath}/events`, handleSse);
|
||||
}
|
||||
|
||||
export function handleSse(req, res) {
|
||||
const companyId = req.user?.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return res.status(400).json({ error: 'No company selected' });
|
||||
}
|
||||
|
||||
req.socket?.setTimeout(0);
|
||||
res.setTimeout?.(0);
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache, no-transform',
|
||||
'Connection': 'keep-alive',
|
||||
'X-Accel-Buffering': 'no' // Instructs NGINX explicitly to disable buffering
|
||||
});
|
||||
res.flushHeaders(); // Ensure headers are sent over HTTP/2 frame immediately
|
||||
|
||||
// Send an initial ping to establish frame
|
||||
res.write(': ping\n\n');
|
||||
|
||||
subscribe(res, { companyId, userId: req.user?.id }, req);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user