0

I have implemented a chat feature using ActionCable. I am now trying to implement a presence status based on the implementation of user appearances in the README.

This documentation mention the following statement:

The #subscribed callback is invoked when, as we'll show below, a client-side subscription is initiated. In this case, we take that opportunity to say "the current user has indeed appeared". That appear/disappear API could be backed by Redis or a database or whatever else.

I can implement an online attribute in my database and update it when the application receives appear/disappear notifications. But I have no guarantee about the reliability of this attribute. It could become out of sync in case of a server failure for example.

How could I implement this in a reliable way?

ybart
  • 876
  • 8
  • 23

1 Answers1

2

Place it in a Redis structure that expires in a certain amount of time (use TTL). If you store it somewhere for an infinite amount of time (like the DB) it can go out of sync. You might argue that you can set all user presence to false on application startup, but that will only work until you run multiple servers or workers. While a user is connected: insert a presence value for this user into Redis every few minutes. Also handle the connection close event to delete the user presence from Redis for higher accuracy than a few minutes.

Pierre Pretorius
  • 2,869
  • 22
  • 21
  • It seems that presence will be defaulting to false after TTL whereas the user might still be online, or am I missing something ? – ybart Oct 12 '15 at 11:02