[BUG] Online User Count displays 0 when users are active #37#40
Closed
soham04010 wants to merge 2 commits intoZenYukti:mainfrom
Closed
[BUG] Online User Count displays 0 when users are active #37#40soham04010 wants to merge 2 commits intoZenYukti:mainfrom
soham04010 wants to merge 2 commits intoZenYukti:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes the issue where the "Online User Count" on the dashboard was displaying incorrect values (often showing
0due 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/src/lib/roomState.tsto store live user counts in memory.ChatServer.tsto instantly detect and remove disconnected users (fixing the bug where users remained "online" after closing the tab).rooms.tsto read counts from shared memory instead of the database.index.ts(to 2000 requests/15min) to prevent429 Too Many Requestserrors caused by dashboard polling.Dashboard.tsxto poll for updates every 3 seconds and changed the UI label from "active now" to "members online".Type of Change
How Has This Been Tested?
I have manually tested the changes using the following scenarios:
429errors.Screenshots
Checklist
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.