javaandroidfirebasefirebase-realtime-databaseconnection

How can I use keep-alive messages to detect a disconnected user from game app?


I'm developing a game app using Android Studio with java, and I'm using Firebase Realtime Database to handle connection and sending messages between users. In this app multiple "guests" connect to 1 "host", basically they all connect to a child of the database under the same game code and send messages there.

My problem is that I want the host to be able to identify a sudden disconnection from one of the guests to eliminate him from the game. Similarly I want some actions done if the hosts disconnects.

In addition to OnDisconnect in my guest activity:

playersRef.child(playerId).onDisconnect().setValue(myEnum.QUIT);

I wanted to implement a keep-alive message system, to cover all cases in which my app may terminate.

I'm not sure how to implement this kind of system to run in the background of my app continuously while the game is taking place. I want the host to keep track of guests and be able to tell when one of those isn't there anymore. Is there a way to implement such class?


Solution

  • I'd start by having each client write a lastSeenAt timestamp at regular intervals, which the other clients can then use to determine when that client was last active/seen and use that to determine how to process their data.

    This is a really broad description though - because how to implement keep-alive is a really broad topic, and not easy to answer concisely here on Stack Overflow.