Skip to content

feat: SSE implementation for real-time event streaming with logging - #108

Open
rahulwebend wants to merge 1 commit into
nomeyy:mainfrom
rahulwebend:feat/sse
Open

feat: SSE implementation for real-time event streaming with logging#108
rahulwebend wants to merge 1 commit into
nomeyy:mainfrom
rahulwebend:feat/sse

Conversation

@rahulwebend

@rahulwebend rahulwebend commented Aug 9, 2025

Copy link
Copy Markdown

This PR adds Server-Sent Events (SSE) support to enable clients to subscribe and receive real-time events pushed from the server.

Features implemented:

  • Clients can connect to /api/sse/stream endpoint and receive server updates in real time.
  • Support for sending arbitrary named events with JSON payloads to individual or multiple connected clients.
  • Connection lifecycle logging (connect, disconnect, error) into logs/connections.log.
  • Graceful handling of client disconnections.
  • SSE headers set to ensure proper event streaming and prevent caching.

Changes Made

  1. Frontend Updates

    • BroadcastPanel: Displays user list with online/offline status in dropdown.
    • SubscriberPanel: Shows received events (message, ping, broadcast) in real time.
  2. Backend Integration

    • Centralized SSE manager to maintain active client connection lifecycle.
    • Clients are identified by userId if provided, otherwise by client.id.
    • Heartbeat ensures connection remains open and is monitored.
    • Disconnections are handled in req.signal.addEventListener("abort").
  3. Client Management

    • Maintains a list of connected clients.
    • Supports sending messages to:
      • A single client by id
      • Broadcast to all connected clients
  4. Robust Error Handling & Logging

    • All SSE connect/disconnect, broadcast, and send actions are wrapped with try/catch and logged using logToFile.
    • Logs:
      • New connections with timestamp & client ID
      • Disconnections with timestamp & client ID
      • Errors during connection/disconnection
  5. API Enhancements

    • GET /api/sse/stream now returns correct connection stream and cleans up on client abort.
    • POST /api/sse/send supports targeted user messages or global broadcast.
    • GET /api/users to fetch all user’s with connectivity status.

Additional Notes

  • SSE keeps a single HTTP connection open for each client — efficient for server → client push.
  • Heartbeats (ping) are sent every 25 seconds to prevent timeouts and detect dead connections.
  • Log file grows over time; consider log rotation for production.

Preview

screenrecord.webm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant