I want to send SSE event only when there is a DB update API called. How do I achieve this? What is the standard market practise to achieve this?
my SSE endpoint looks like this
app.get('/send-events', (req, res) => {
const headers = {
Connection: "keep-alive",
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
};
res.writeHead(200, headers);
const data = `data: ${new Date()}\n\n`;
res.write(data);
});
i want to trigger the above api when another api is being called. Eg below
app.post('/update-db', (req, res) => {
res.send('db-updated');
//perform db update
//send the latest data thru sse endpoint
});
CodePudding user response:
It seems by default SSE doesn't work with multiple clients/browser tabs. In order to achieve that we have to send the event to all the clients that are currently connected/listening to the server
This code sample solves my problem