Twake icon indicating copy to clipboard operation
Twake copied to clipboard

Fix user live status

Open RomaricMourgues opened this issue 2 years ago • 5 comments

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 and gr.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

  1. Update the online-user entity to save also if user is currently connected or not (boolean)
  2. 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).
  3. 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.

RomaricMourgues avatar Jul 11 '22 09:07 RomaricMourgues

Ready to go @oubchid

RomaricMourgues avatar Jul 11 '22 10:07 RomaricMourgues

@oubchid I reviewed your work on the PR, it is good, I put a comment. Next step is company scope in connected / disconnected rooms.

RomaricMourgues avatar Jul 22 '22 09:07 RomaricMourgues

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”Screenshot 2022-08-25 at 13.21.29.png

tprudentova avatar Aug 26 '22 09:08 tprudentova

  1. When the user is online instead of “online” it’s says “online a few seconds ago” Screenshot 2022-09-22 at 00.33.37.png
  2. If the name of the user is too long, the status info gets cropped Screenshot 2022-09-22 at 09.57.16.png Screenshot 2022-09-22 at 01.34.15.png
  3. 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”
  4. 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 activityScreenshot 2022-09-22 at 01.28.28.png 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"

tprudentova avatar Sep 22 '22 07:09 tprudentova

  • 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)

RomaricMourgues avatar Sep 22 '22 07:09 RomaricMourgues

Screenshot 2022-09-23 at 14 20 10 Considering issue 4, yep, the dot is green

tprudentova avatar Sep 23 '22 11:09 tprudentova

@tprudentova did you see any issues on this one ? We fixed it last week.

RomaricMourgues avatar Sep 30 '22 06:09 RomaricMourgues

Screenshot 2022-09-30 at 10.23.27.png Info still gets cropped when the name is too long, everything else looks fine.

tprudentova avatar Sep 30 '22 07:09 tprudentova