Skip to content

[BUG] Online User Count displays 0 when users are active #37#40

Closed
soham04010 wants to merge 2 commits intoZenYukti:mainfrom
soham04010:fix/online-user-count
Closed

[BUG] Online User Count displays 0 when users are active #37#40
soham04010 wants to merge 2 commits intoZenYukti:mainfrom
soham04010:fix/online-user-count

Conversation

@soham04010
Copy link

Description

This PR fixes the issue where the "Online User Count" on the dashboard was displaying incorrect values (often showing 0 due to rate limiting or retaining stale user counts after they left).

Previously, the user count relied on querying the database for messages sent in the last 15 minutes. This has been replaced with a real-time "Shared Memory" approach that tracks actual active WebSocket connections.

Key Changes:

  • Backend: Created backend/src/lib/roomState.ts to store live user counts in memory.
  • Chat Server: Implemented "Lazy Cleanup" in ChatServer.ts to instantly detect and remove disconnected users (fixing the bug where users remained "online" after closing the tab).
  • API: Updated rooms.ts to read counts from shared memory instead of the database.
  • Rate Limiting: Increased the server rate limit in index.ts (to 2000 requests/15min) to prevent 429 Too Many Requests errors caused by dashboard polling.
  • Frontend: Updated Dashboard.tsx to poll for updates every 3 seconds and changed the UI label from "active now" to "members online".

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🧹 Code refactoring (no functional changes)
  • ⚡ Performance improvement
  • ✅ Test improvement
  • 🔧 Build or CI/CD configuration changes

How Has This Been Tested?

I have manually tested the changes using the following scenarios:

  1. Join/Leave Test: Opened the app in multiple tabs. Confirmed that the "Online Count" increases immediately when a tab joins and decreases immediately when a tab is closed.
  2. Rate Limit Test: Monitored the Network tab in Developer Tools to ensure the polling requests (every 3s) no longer return 429 errors.
  3. Stale Data Test: Verified that users who leave without sending a message are correctly removed from the count.

Screenshots

image image image

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
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Additional Information

Note to Reviewers: The polling interval on the frontend is set to 3 seconds. The backend rate limit was increased to accommodate this. If we expect high traffic (hundreds of concurrent users on the same IP), we may need to increase the polling interval or further adjust the rate limit.

Learning Outcomes

Learned how to synchronize state between a WebSocket server and a REST API using shared memory in Node.js, and how to handle "Ghost Connections" caused by React Strict Mode re-renders.

@soham04010 soham04010 changed the title Fix/online user count [BUG] Online User Count displays 0 when users are active Feb 11, 2026
@soham04010 soham04010 changed the title [BUG] Online User Count displays 0 when users are active [BUG] Online User Count displays 0 when users are active #37 Feb 11, 2026
@soham04010 soham04010 closed this Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant