Closes issue #17 Add SSE heartbeat and stale-client cleanup strategy#52
Closes issue #17 Add SSE heartbeat and stale-client cleanup strategy#52Kingajong wants to merge 4 commits into
Conversation
…e-client-cleanup Add SSE heartbeat and stale-client cleanup for event stream
|
@Kingajong is attempting to deploy a commit to the flamki's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactors SSE to track clients in a per-connection Map with per-client ChangesSSE Client Tracking and Readiness Update
sequenceDiagram
participant Client
participant Server
participant sseClientsMap
participant writeSse
Client->>Server: GET /api/events (open SSE)
Server->>sseClientsMap: create clientId, store {res, lastWriteAt}
Server->>writeSse: send "connected" event
Note over Server,writeSse: heartbeat interval iterates sseClientsMap
Server->>writeSse: send "heartbeat" to clients
writeSse-->>sseClientsMap: update lastWriteAt or remove stale/error clients
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/server.js`:
- Around line 29-34: The removeClient function should guard against calling
client.res.end() on an already-destroyed stream to avoid exceptions; update
removeClient (referencing sseClients, client, and client.res.end) to first
remove the client from sseClients, then check the response stream state (e.g.,
writableEnded/writableFinished/destroyed) and call client.res.end() only if it
is safe, and wrap the end() call in a try/catch to swallow and log any errors so
cleanup still completes even if the socket is already closed.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
|
@Kingajong fix this potential issue |
|
@Kingajong Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
@Kingajong kindly resolve this confict |
|
@Flamki Good Day Sir i am still waiting for your approval |
|
@Kingajong kindly resolve this confict |
version to expose component-level readiness
CLOSES #17
SSE clients were tracked as bare response objects without heartbeat or backpressure handling, which can allow stale/disconnected clients to accumulate in memory over long runs.
The dashboard needs to reconnect cleanly after transient network blips and avoid unbounded memory growth from dead EventSource connections.
Replace the plain array with a keyed
Mapof clients and addwriteSse(clientId, event)andremoveClient(clientId)helpers to manage lifecycle and writes.Guard all SSE writes with
try/catch, observeres.write()return values, and attach adrainlistener to handle per-client backpressure.Emit a
heartbeatevent every 30s and evict clients that have not accepted writes for more than 90s to remove stale connections.Update the
/api/eventshandler to assign per-client IDs, send the initialconnectedevent via the guarded writer, and clean up on bothcloseanderrorevents.Ran
node --check src/server.jswhich completed successfully.No new automated tests were added in this change.
Summary by CodeRabbit
Bug Fixes
Improvements