What's a good way to display online users?

  • Apollo server can be setup with subscriptions from 'subscriptions-transport-ws'
  • React offers the useSubscription hook
  • Apollo client offers the WebSocket Link
  • Neo4j 4.X offers reactive sessions
  • Google pubsub can be used to handle subscriptions in a multi-servers situation when the kubernetes cluster scales the pod. I'd like to avoid adding a new element like Redis or Kafka to the already complex stack that is GRANDstack on Kubernetes.

Right now, I'm hesitating between 2 options:

  • Keep it simple on the server. 2 subscriptions userOnline and userOffline, and each client must update a local array of online users via a cache merge type policy. Not sure how to re-render the cached array data when one of the subscriptions gets new data.
  • Keep it simple on the client. A single subscription usersOnline returning the list of users, and the client only needs to compare previous and new data to figure out who's just logged in and who's just logged out. On the server side, the subscription triggers a reactive session transaction to toggle a User.isOnline boolean, and returns the list of all Users matching isOnline=true.

Which way would you choose for such a feature? Or do you have an alternative suggestion that might be simpler/more efficient?

I might have found a simpler solution.

  • Server updates the User.isOnline value in the db on subscriptions.onConnect and onDisconnect (I suppose the driver's asynchronous session is fine, no need for the reactive one).
  • Client calls useLazyQuery to get all online users, with the hook option subscribeToMore to get further updates (the internals of subscribeToMore are obscure to me, will need to dive into it).

It's not going to help you right now, however, I've got it on pretty good authority that the GRANDstack will be adding in subscription support soon. Which could possibly make this quite a bit easier.

1 Like