Skip to content

onDisconnect() triggers immediate online status after going offline #7653

@yanoyuki

Description

@yanoyuki

Operating System

MacOS 13.3.1 (Intel)

Browser Version

Google Chrome 117.0.5938.92

Firebase SDK Version

9.22.1

Firebase SDK Product:

Database

Describe your project's tooling

React application. Built using Vite and hosted on Google App Engine.

Describe the problem

We have implemented a mechanism to detect user presence using ref(db, '.info/connected').

Starting from a certain day, in various user environments, we can no longer accurately detect when a user goes offline.

After a user closes their PC and goes offline, the SDK notifies us of the offline status.
However, within 1-2 seconds, it reports the status back as online, even though the user's PC remains closed.

Steps and code to reproduce issue

In our React application, we detect user presence as follows:

  const connectedRef = ref(db, '.info/connected')

  const offlineData = {
    state: "offline",
    updatedAt: serverTimestamp(),
  }
  const onlineData = {
    state: "online",
    updatedAt: serverTimestamp(),
  }

  const presenceRef = ref(db, `presence/${uid}`)

  onValue(connectedRef, (snapshot) => {
    const isOnline = snapshot.val()
    if (isOnline === false) {
      return
    }
    onDisconnect(presenceRef)
      .set(offlineData)
      .then(() => {
        set(presenceRef, onlineData)
      })
  })

Around September 24-25, 2023, the following issue suddenly began occurring:

  • Action:
    Putting MacOS into sleep mode.

  • Expected Result:
    Realtime Database sets data with state: "offline" .

  • Actual Result:
    Within 1-2 seconds of the Realtime Database setting data with state: "offline", even though MacOS remains in sleep mode, the data is reset to state: "online".

Similar behavior is observed when "closing the MacOS laptop". The system briefly goes to "offline" state, but then almost immediately resets to "online", preventing the app from correctly detecting an offline state.

This malfunction appeared suddenly around September 24-25, 2023. Before that, this source code had been working as expected for over a year, and there have been no changes to the source code on our end.

Has there been any changes on the server side regarding the conditions under which onDisconnect() is triggered for:

const connectedRef = ref(db, '.info/connected')

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions