Cogency.io

submited by
Style Pass
2021-06-05 13:30:07

One of the major necessities with today's technologies is how to efficiently keep track of the online users. Many application features could be turned on/off depending on whether the user is online or offline.

Traditionally, the approach to solve this problem has been to implement some type of pinging mechanism on the client-side where the browser sends pings to the server ( in our case a WebSocket server ) when the user comes online. The browser will keep on sending pings in regular intervals to let the server know that the user is still online. Once the user logs off the server is notified yet again that the user is now offline. This method is flawed because it does not take into consideration several possibilities such as

To overcome this problem a purging mechanism ( on the server-side ) would be implemented that runs every few minutes to check if the last user's timestamp is older than a minute, and if it is removes the user from the online_users array and broadcasts the new list. In theory this should work but in practice this approach introduced gaps when the user might already be offline but the purging function has not yet run and thus the system still thinks that user is online.

Well, I believe there is a better solution out there and here is my implementation of it (the inspiration for this implementation came from a stackoverflow post here https://stackoverflow.com/a/52559338/2469475).

Leave a Comment
Related Posts