0

Using the Firebase Javascript library, I can use the onDisconnect() method to schedule a database update when the client is disconnected.

Google has released a set of Polymer Elements called PolymerFire, which make hooking Polymer apps and Firebase very easy.

Has anybody figured out how to use the onDisconnect method from inside a Polymer app using PolymerFire?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
abendigo
  • 954
  • 1
  • 8
  • 31

1 Answers1

2

I found the answer!

So, at the start of my app, I initialized the firebase-app like this:

<firebase-app name="notes" ...></firebase-app>

Then, later in my app, I add a firebase-document to watch my connection status:

<firebase-document app-name="notes" path="/.info/connected" data={{conStatData}}>
</firebase-document>

and I added an observer on conStatData:

observers: ['_conStat(user, conStatData)'],

And finally, the payoff code:

_conStat: function(user, status) {
      if (status === true) {
          firebase.app('notes').database().ref('/presence/' + user.uid).set('online')
          firebase.app('notes').database().ref('/presence/' + user.uid).onDisconnect().set('offline')
      }
  },
abendigo
  • 954
  • 1
  • 8
  • 31
  • 1
    I would add `user` to the observer parameters and use `user.uid` (instead of `this.user.uid`) in case `user` is not yet defined (e.g., the user is not yet logged in). – tony19 Dec 31 '16 at 05:40