I am creating a flutter app, it would be essential to detect almost immediately a user goes offline from the realtime/firestore database and notify other users of the same.<
There are two ways a client can disconnected:
a clean disconnect, where the client lets the server know that it's disappearing.
a dirty disconnect, where the client disappears and it's up to the server to detect this condition.
For clean disconnects, the onDisconnect
writes you defined will run immediately.
Dirty disconnects depend on socket time-outs, which means it may take multiple minutes before your onDisconnect
writes happen. There is nothing you can do about this behavior, as it's an inherent part of how sockets work.
If you want a faster way to detect what clients are still connected, you can write a keep-alive in the database. Essentially: every 10 seconds write a sentinel value from every client.