我只想在呼叫資料庫更新 API 時發送 SSE 事件。我如何實作這一目標?實作這一目標的標準市場慣例是什么?
我的 SSE 端點看起來像這樣
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);
});
我想在呼叫另一個 api 時觸發上述 api。例如下面
app.post('/update-db', (req, res) => {
res.send('db-updated');
//perform db update
//send the latest data thru sse endpoint
});
uj5u.com熱心網友回復:
默認情況下,SSE 似乎不適用于多個客戶端/瀏覽器選項卡。為了實作這一點,我們必須將事件發送到當前連接/監聽服務器的所有客戶端
此代碼示例解決了我的問題
https://www.digitalocean.com/community/tutorials/nodejs-server-sent-events-build-realtime-app#step-2-testing-the-backend
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517181.html
上一篇:已達到套接字最大fds
