Skip to content

Fix/online user count#41

Open
soham04010 wants to merge 3 commits intoZenYukti:mainfrom
soham04010:fix/online-user-count
Open

Fix/online user count#41
soham04010 wants to merge 3 commits intoZenYukti:mainfrom
soham04010:fix/online-user-count

Conversation

@soham04010
Copy link

Description

This PR implements a robust Real-Time Online User Count system, replacing the previous database-polling method which caused stale data and rate-limiting issues.

The new system uses a Shared Memory Architecture to track active WebSocket connections instantly. It also resolves critical stability bugs including "ghost users" (users appearing online after leaving) and frontend connection loops.

🎯 Key Features & Fixes

1. Real-Time "Shared Memory" Architecture

  • New File: Created backend/src/lib/roomState.ts to store live user counts in memory (Map<string, number>).
  • Why: This allows the API to serve user counts instantly (0ms latency) without hitting the database, preventing performance bottlenecks and stale data.

2. Backend Stability (ChatServer.ts)

  • Ghost User Fix: Implemented "Lazy Cleanup" logic. The server now actively checks for dead sockets whenever counting users, preventing the count from getting stuck (e.g., showing 2 users when only 1 is online).
  • Strict Disconnect Handling: Added logic to ensure we only remove the specific socket that disconnected. This prevents a user from accidentally deleting their own new connection if they refresh the page quickly.
  • Safe Iteration: Fixed a bug where iterating over a Set while deleting from it caused users to be skipped. Replaced with safe Array.from() iteration.

3. Frontend Reliability (useWebSocket.ts)

  • Stale Closure Fix: Refactored the hook to use useRef for event callbacks (onMessage, onConnect). This prevents the WebSocket from disconnecting and reconnecting on every React render.
  • Explicit Leave Signal: Added a cleanup function that sends a type: 'leave' message immediately before the component unmounts, ensuring the count drops the moment a user closes the tab.

4. Infrastructure (index.ts)

  • Rate Limit Adjustment: Increased the API rate limit to 2000 requests per 15 minutes (previously ~100). This safely accommodates the Dashboard polling (every 3 seconds) without triggering false-positive 429 Too Many Requests errors.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • ⚡ Performance improvement
  • 🧹 Code refactoring

How Has This Been Tested?

I have validated these changes with the following scenarios:

  1. Instant Leave: Opened a room in a new tab and closed it immediately. Verified the count dropped instantly on the dashboard.
  2. Refresh Persistence: Refreshed the chat page multiple times. Confirmed the user count remained stable (did not duplicate to "2 online").
  3. Notifications: Verified that "User joined" and "User left" system messages appear correctly in the chat window.
  4. Network Stability: Monitored the Network tab to ensure polling requests return 200 OK and no longer trigger 429 errors.

Screenshots

Screenshot 2026-02-11 173738 Screenshot 2026-02-11 174801 Screenshot 2026-02-11 174819

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings

Learning Outcomes

Implemented a hybrid state management system syncing WebSocket events with REST API polling using in-memory storage for high-performance real-time updates.

@ayushHardeniya ayushHardeniya added apertre3.0 Apertre 3.0 open source program medium features involving multiple files or logic. backend Server-side logic and APIs labels Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apertre3.0 Apertre 3.0 open source program backend Server-side logic and APIs medium features involving multiple files or logic. triage-needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments