Firebase persistance - onDisconnect with multiple browser windows

前端 未结 2 1373
北荒
北荒 2021-02-09 05:27

We\'re writing an app that monitors online presence. There are multiple scenarios where we need the user to have more than one browser window open. We\'re running into a probl

2条回答
  •  青春惊慌失措
    2021-02-09 06:11

    Another simple solution (Using angularfire2, but using pure firebase is similar):

    const objRef = this.af.database.list('/users/' + user_id);
    const elRef = objRef.push(1);
    elRef.onDisconnect().remove();
    

    Each time a new tab is opened a new element is added to the array. The reference for "onDisconnect" is with the new element, and only it is excluded.

    The user will be offline when this array is empty.

提交回复
热议问题