Conversation
|
Server Overview (server.js) Key Responsibilities 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.
Key Exports 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. Core Features 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. Validates clientId. Optionally fetches user details from DB. Sends event via sseService.sendToClient.
|
No description provided.