Search code examples
firebasepolymerfirebase-realtime-databasepolymerfire

How can I use the onDisconnect method with PolymerFire?


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?


Solution

  • 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')
          }
      },