2
2 Comments

How can you build a feature that can show you if your friends are online or not?

In a chat application built with node js, how can you build a feature that can show you if your friends are online or not? Similar to what Facebook messenger uses. I have been looking at socket io, but I am not sure if this will be the right solution.

Thanks

on March 12, 2020
  1. 1

    Using WebSockets is definitely the ideal solution, as your app won't need to rely on polling to determine when a user is online. As soon as a user closes your app, their connection to the WebSockets service is closed. Other users on the same WebSockets channel can listen out for events that show who has come online and who has gone offline. What events are transmitted to users is entirely up to you, as the developer.

    Polling is certainly another way to accomplish the capability to show who is online, but you would need a process or script running in the background on the client machine to check for updates on a regular basis. It's not the most ideal solution now that we have WebSockets to deal with this in a much cleaner and more coherent way.

    Ultimately choose the option that makes the most sense for your application and your use case. I can't envisage a scenario where polling would be more appropriate unless it's to save time. I wouldn't recommend it, but it's an option nonetheless.

  2. 1

    You'd want to toggle a value in your database on page load and when the user closes the browser window. I think this can be done with socket.io, although it's even easier with Firebase. See https://firebase.google.com/docs/firestore/solutions/presence