unexplained firebase real time database timeout getting data from client

Viewed 78

I'm having a weird issue where some of my users are getting timeouts when requesting their account information from the firebase realtime database (super small objects).

Call to get user data

export const getUserData = (userId) =>
  new Promise((resolve, reject) => {
    onValue(
      ref(db, '/users/' + userId),
      (snapshot) => resolve(snapshot.val()),
      (err) => {
        console.error('err', err)
        reject(err)
      },
      { onlyOnce: true }
    )
  })

User login hook:

useEffect(() => {
    // onAuthStateChanged returns an unsubscribe method
    const unsubscribe = onAuthStateChanged(auth, (user) => {
      setCurrentUser(user)
      if (user) {
        const getUserDataTimeout = setTimeout(() => {
          // FAILS HERE FOR SOME USERS
          setLoading(false)
          setUserDataError(
            'User data request timeout, try refreshing the page or another browser, and please contact us at support@example.com.'
          )
           // 5s is irrelevant, after 1s it'll never complete
        }, 5000)
        getUserData(user.uid)
          .then((userData) => {
            setLoading(false)
            setUserData(userData)
            clearTimeout(getUserDataTimeout)
          })
          .catch((err) => {
            console.error("couldn't get user data", err)
            setLoading(false)
            setUserDataError(err.message)
            clearTimeout(getUserDataTimeout)
          })
      } else {
        setLoading(false)
      }
    })
    return unsubscribe
  }, [])

When logging into that user's account, I have no issues. Their issue seems to go away when switching from latest Chrome to a different browser. I feel like it has something to do with web sockets being blocked by the network. I tried throttling the connection and it's still lightning fast. Any advice on how to debug, or what could be causing it?

0 Answers
Related