Twake
Twake copied to clipboard
Fix user live status
Context
The red/green status of users on Twake is broken.
Investigation
So the issue is about our library (socket.io) not being able to detect a user disconnected from all nodes in a multi-node environment. We do have events for when user starts websocket connection and stop websocket connection but it is local to the node and doesn't allow us to know if the user is not also connected somewhere else. Ref: https://stackoverflow.com/questions/30625556/socket-io-how-to-count-clients-in-a-room-with-socket-io-redis-adapter
So the current implementation is to set the user as logged in when it open any websocket connection, and then a cron is looking at every users every minutes to update the disconnected ones. Today the issue is simply that the cron read also the disconnected user locally, so of course we are always disconnected as nobody is connected to all the nodes. (I oversimplified the last info because in fact we don't look up all the users but only the one that did create a connection at least one during the runtime of the node but the result is the same).
We will not continue with this implementation as the potential fix will also probably make outdated the cron usage (I hope)
Some paths:
- Where users last connection status is stored: twake/backend/node/src/services/online/entities/user-online.ts
- Where users event are used:
gr.platformServices.websocket.onUserConnected
andgr.platformServices.websocket.onUserDisconnected
twake/backend/node/src/services/online/service/index.ts
New implementation proposal
Step 1
Fix the current implementation using https://socket.io/docs/v3/server-api/#namespaceallsockets
- Update the online-user entity to save also if user is currently connected or not (boolean)
- When user disconnect from websockets (the event is already implemented) count the number of socket connections active in the room
/me/{user-id}
(only "user-id" is connected to this room so counting the number of connection will tell us if the user is online or not). Depending on the result of this count we proceed or not (real disconnect or not real disconnect). - Remove the cron because it is not used anymore
Step 2
Change the /users/online room to a /users/online/{company_id} room (separate per companies)
Actually this should be straitforward as we can get the user companies from the cache of the user entity directly. Don't forget to change it in frontend too and automatically join /users/online/{company_id}
when router changes company in the hook.
To do
-
[x] Fix green/red STEP 1 - as described in the paragraphs before
-
[x] Fix green/red STEP 2 - as described in the paragraphs before
-
[x] New feature - In the formatUser (twake/backend/node/src/utils/users.ts) method, add the last connection date from the online-user entity (twake/backend/node/src/services/online/entities/user-online.ts) in order for the frontend to always have this information.
Ready to go @oubchid
@oubchid I reviewed your work on the PR, it is good, I put a comment. Next step is company scope in connected / disconnected rooms.
Hi @RomaricMourgues
After user1 has the page opened for a while but shows no activity, they appear offline for user2 and the status doesn’t change after user1 produces actions within their page (a.i. switching company, workspace, chat, sending message), it only changes once user1 refreshes the page
In this situation user1 also sees themselves offline in the list of direct chats, even though the status on top is “online”
- When the user is online instead of “online” it’s says “online a few seconds ago”
- If the name of the user is too long, the status info gets cropped
- The counter is a bit inconsistent, for instance, the user has been online 44 minutes ago -> “online 44 minutes ago”; the user has been online 50 minutes ago -> “online an hour ago”
- When user 1 is currently online (has the tab opened, has a green label next to their name) but hasn’t shown any activity within the app, for user 2 they’re seen as “online period of recent activity”
The user in screenshot is online and has a green badge next to their name. When user 1 refreshes the page, they become "online a few seconds ago"
- 1 will be fixed thanks
- 2 will be replaced by an ellipsis ("...")
- 3 it is on purpose, we don't need precision here
- 4 if the dot is green then we will fix it, but else this information gets refreshed only after few minutes so it is on purpose too (but with the green dot rule it should not be a problem anymore)

@tprudentova did you see any issues on this one ? We fixed it last week.
Info still gets cropped when the name is too long, everything else looks fine.