Skip to content

initial commit#110

Open
talhatariq1796 wants to merge 1 commit intonomeyy:mainfrom
talhatariq1796:feature-demo-server-side-events
Open

initial commit#110
talhatariq1796 wants to merge 1 commit intonomeyy:mainfrom
talhatariq1796:feature-demo-server-side-events

Conversation

@talhatariq1796
Copy link

No description provided.

@talhatariq1796
Copy link
Author

Server Overview (server.js)
This is the main entry point of the backend service.

Key Responsibilities
Loads environment variables.

Sets up Express app with CORS and JSON parsing.

Logs all incoming requests with timestamps.

Registers route groups:

/sse → Server-Sent Events routes

/api → REST API routes

Health check endpoint: /health

Root endpoint: /

Handles 404 and server errors.

Graceful shutdown on SIGTERM/SIGINT.

  1. Database Layer (db.js)
    This uses pg’s connection pooling to interact with a PostgreSQL database.

Key Exports
query(text, params) → Executes SQL queries with logging & error handling.

getClient() → Gets a pooled DB client (for transactions).

transaction(callback) → Runs multiple DB operations atomically.

testConnection() → Verifies DB connection & logs current DB time.

closePool() → Closes pool on shutdown.
SSE Service (sseService.js)
Manages real-time client connections using Server-Sent Events.

Core Features
addClient(res, userId?, sessionId?) → Registers a new SSE connection.

removeClient(clientId) → Closes and removes a connection.

sendToClient(clientId, eventType, data) → Sends event to a specific client.

sendToUser(userId, eventType, data) → Sends to all clients of a user.

broadcast(eventType, data) → Sends event to all clients.

Heartbeat mechanism → Sends ping events every 30s to keep connections alive.

getStats() → Returns connection statistics.

cleanupStaleConnections(maxAge) → Removes clients inactive for too long.
Flow:

Validates clientId.

Optionally fetches user details from DB.

Sends event via sseService.sendToClient.
Flow:

  1. Validates clientId.

  2. Get User by ID
    GET /api/user/:id
    Fetches a single user record from the database.

  3. Get All Users
    GET /api/users
    Returns all users from DB, ordered by id.

  4. Send Notification to All Clients of a User
    POST /api/notify/user/:userId
    Broadcasts to all SSE connections for a given user.

  5. Broadcast Notification to All Clients
    POST /api/notify/broadcast
    Sends an event to all connected clients.

  6. Trigger Test Notification
    POST /api/test-notification
    Broadcasts a sample test notification for demo purposes.

  7. Clean Up Stale Connections
    POST /api/cleanup
    Removes connections idle beyond the configured timeout (default: 5 minutes).

  8. API Health Check
    GET /api/health
    Checks DB connection and returns SSE stats.

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