Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

503
Views
Firebase Realtime Database onDisconnect() Consistency

I've got a near full-functioning realtime database presence system as described here but I am encountering a few issues. It seems that people are remaining online even after having disconnected a while ago. I'm not sure why but I've opened up my security rules to unauthenticated requests temporarily to no avail. It could be due the bug described here.

If the issue is the ladder, what would be the proper JavaScript implementation to avoid this issue? Is it a good solution to recreate the onDisconnect listener every 60 minutes? For reference, the code I'm using is shown below:

export function selfPresence(byUpdate) {
  onValue(ref(rtdb, '.info/connected'), (snap) => {
    isDBConnected = snap.val();
    if (snap.val() === true) {
      presencecon = push(ref(rtdb, `users/${user.uid}/connections`)); // Create new presence thing.
      onDisconnect(presencecon).remove();
      set(presencecon, true);
      onDisconnect(ref(rtdb, `users/${user.uid}/lastOnline`)).set(serverTimestamp());
      onDisconnect(ref(rtdb, `users/${user.uid}/currentlyListening`)).remove();
    }
  });
}

MY SOLUTION

  • I could not figure out how to get RTDB to act consistently so I opted for the code below instead. Basically, it just updates a boolean at a specific path so that if/when the issue DOES happen, the next time the user is online, it will overwrite the failure with new online listener.
onValue(ref(rtdb, '.info/connected'), (snap) => {
    isDBConnected = snap.val();
    if (snap.val()) {
        onDisconnect(presencecon).set(false);
        presencecon = ref(rtdb, `users/${user.uid}/online`);
        isDBConnected = snap.val();
    }
    else {
        remove(presencecon);
    }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The below code updates a boolean at a specific path so that if/when the issue DOES happen, the next time the user is online, it will overwrite the failure with a new online listener.

onValue(ref(rtdb, '.info/connected'), (snap) => {
    isDBConnected = snap.val();
    if (snap.val()) {
        onDisconnect(presencecon).set(false);
        presencecon = ref(rtdb, `users/${user.uid}/online`);
        isDBConnected = snap.val();
    }
    else {
        remove(presencecon);
    }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!