0

Please I'm trying to detect if the user is online or offline and I wrote the code below.


    Database.database().isPersistenceEnabled = true
        let presenceRef = Database.database().reference(withPath: "disconnectmessage");
        // Write a string when this client loses connection
        presenceRef.onDisconnectSetValue("I disconnected!")
        
        presenceRef.onDisconnectRemoveValue { error, reference in
          if let error = error {
            print("Could not establish onDisconnect event: \(error)")
          }
        }
        
        let connectedRef = Database.database().reference(withPath: ".info/connected")
        connectedRef.observe(.value, with: { snapshot in
          if snapshot.value as? Bool ?? false {
              
            print("Connected")
              if Auth.auth().currentUser != nil {
                  OnlineOfflineService.online(for: (Auth.auth().currentUser?.uid)!, status: true)
              }
              
          } else {
            print("Not connected")
              if Auth.auth().currentUser != nil {
                  OnlineOfflineService.online(for: (Auth.auth().currentUser?.uid)!, status: false)
              }
          }
        })

and I tried to open the permission.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

I tried also to use.

func applicationWillTerminate(_ application: UIApplication) {
        if Auth.auth().currentUser != nil {
            OnlineOfflineService.online(for: (Auth.auth().currentUser?.uid)!, status: false)
        }
 }

The question is, how can I detect if the user is online or offline? Any suggestions, please??

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You haven't said what the problem is, so it's a little unclear what you're after, but you may want to check the documentation on this issue: https://firebase.google.com/docs/firestore/solutions/presence – jnpdx Sep 05 '22 at 16:26
  • Please I'm wonder about how can I detect if the user is online or offline Thank – Alama Yasein Sep 05 '22 at 16:30
  • Attempt to connect. If you get an error that there is no network, the user is offline. – matt Sep 05 '22 at 16:33
  • Thanks @matt What about if the user closed the app? – Alama Yasein Sep 05 '22 at 16:41
  • Then there is no question to answer. The app is not running so there is no "online" or "offline" to worry about. – matt Sep 05 '22 at 16:43
  • 1
    The security rules you're showing apply to Cloud Firestore, while the code you're showing is accessing the Realtime Database. While both databases are part of Firebase, they're completely separate, and the security rules for one don't apply to the other. To fix the error, you will have to set the rules for the Realtime Database. For a walkthrough of how to do that, see https://stackoverflow.com/a/52129163 – Frank van Puffelen Sep 05 '22 at 18:39
  • I think your tag `google-cloud-filestore` is incorrect. It should probably be the Firestore tag or the one for the Realtime Database. I didn't want to change it in case there was more to the question. – Jay Sep 08 '22 at 17:47
  • The question needs clarity; you want to detect when a user is online or offline - for the local app that's online or offline or so that (for example) other users know if this user is connected or not? – Jay Sep 08 '22 at 17:49

0 Answers0