question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Manage online offline status

See original GitHub issue

Is it possible to use the onDisconnect feature described on this website?

https://firebase.google.com/docs/database/web/offline-capabilities

// any time that connectionsRef's value is null (i.e. has no children) I am offline
var myConnectionsRef = firebase.database().ref('users/joe/connections');

// stores the timestamp of my last disconnect (the last time I was seen online)
var lastOnlineRef = firebase.database().ref('users/joe/lastOnline');

var connectedRef = firebase.database().ref('.info/connected');
connectedRef.on('value', function(snap) {
  if (snap.val() === true) {
    // We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)
    var con = myConnectionsRef.push();

    // When I disconnect, remove this device
    con.onDisconnect().remove();

    // Add this device to my connections list
    // this value could contain info about the device or a timestamp too
    con.set(true);

    // When I disconnect, update the last time I was seen online
    lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
  }
});```

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Dashuecommented, Sep 30, 2017

This is how I’m using it, is this what you’re looking for?

        firebase.addValueEventListener(data => {
            if (data.value) {
                console.log('Status: Online');
            } else {
                console.log('Status: Offline');
            }

        }, '.info/connected');

Although if you’re trying to build a presence system, you might want to look at this link, as what you can do in the offline callback is limited (as you’re offline): https://firebase.googleblog.com/2013/06/how-to-build-presence-system.html

0reactions
EddyVerbruggencommented, Jan 30, 2019

Indeed it isn’t a function because it’s not currently implemented (nor documented).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle users' online and offline status in Firebase
I want to handle the online and offline status in my webapp. So that users can see who is online and who not....
Read more >
Determine Realtime User Online/Offline Status Using Socket.io
In this video you will be able to integrate socket & determine user status. If a user is logged in then it will...
Read more >
Detecting If a User is Online/Offline with JavaScript - DEV ...
In this post, we are going to look at a super-easy way to check if the user is connected to the internet and...
Read more >
How smart things manage the online/offline status
When we set the power control to off the device such as Light/ TV still returns the online status and we can turn...
Read more >
Detecting Online/Offline Status in Javascript - Level Up Coding
We can detect if the user is online or offline by using the online property on the navigator object which will return true...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found