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

96
Views
Javascript Firebase real time database onDisconnect

So when i'm closing my browser i would like to add

         const timestamp = Date.now();
         const username = "MyUser";
         const message = "I have logged out";
         db.ref("messages/222222222222222" + timestamp).set({
           username,
           message,
         });

I have tried:

const presenceRef = firebase.database().ref("disconnectmessage");
presenceRef.onDisconnect().set("I disconnected!");

But i can't add my code in to it:

db.ref("messages/222222222222222" + timestamp).set({
           username,
           message,
         });

How can i add my own event listener to onDisconnect?

This not working for me for some reason:

var connectedRef = firebase.database().ref(".info/connected");
connectedRef.on("value", (snap) => {
  if (snap.val() === true) {
    console.log("connected");
  } else {
    //My code here is not working
    // When i'm closing the broswer it doesn't add it to 
    // messages 
  }
});

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

0

You should be able to just do this:

db.ref("messages/222222222222222" + timestamp).onDisconnect().set({
  username,
  message,
});

Your last snippet will not work, because by the time the .info/connected gets set to false, the connection to the server has been lost already so you can't write to the remote database anymore

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!