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

70
Views
The website doesn't show the status of activity correctly

I am building a chat website. Everything works really well except the activity status. At first it seems to work, but sometimes it gets glitchy, especially when a user sends a message. It automatically changes to the previous value (e.g. "online" goes to "10 minutes ago" which is the previous value from the database. I am using JS and Firebase. It has 4 parts that are responsible for the status of activity. Even tho the database contains the good value of activity something in my code is broken that it shows the wrong value.

Here is my code:

// logging and disconnecting
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION);
firebase.auth().onAuthStateChanged(function (user) {
  if (user)
  {
      currentUser.style.display = "block";
      currentUser.innerHTML += firebase.auth().currentUser.email;
      document.getElementById("logout").style.display = "block";
      username = firebase.auth().currentUser.displayName;

// updating the user's online status
      let connectionsRef = db.ref("status/" + username + "/connections");
      let lastOnlineRef = db.ref("status/" + username + "/lastOnline");
      let connectedRef = db.ref(".info/connected");

      connectedRef.on("value", function (snapshot) {
          if (snapshot.val() === true) {
              const connection = connectionsRef.push();
              connection.onDisconnect().remove();
              connection.set(true);
              lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
          }
      });
  }
});

// setting user's last-online status (function)
function lastOnline()
{
  let lastOnline = db.ref("status/" + receiver + "/lastOnline");
  lastOnline.on("value", function (snapshot) {
      const lastOnlineDate = snapshot.val();
      const displayedLastOnline = timeSince(lastOnlineDate);
      document.getElementById("status").innerHTML = "last seen: " + displayedLastOnline + " ago";
  });
}

// logging out (function)
function logout()
{
  firebase.auth().signOut().then(() => {
          window.location.replace("./index.html");
  }).catch((error) => {});
}
about 4 years ago · Juan Pablo Isaza
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!