Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

505
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda