Context:
Android game. (Two)Users can create or join room with an unique room name.
Goal:
- if user 1 disconnects while user 2 is in room. user 1 can still join back into room.
- if user 1 and 2 disconnect, firebase should remove room name from storage.
Issue:
How to do goal 2? Because if user close the app, then the app can't tell firebase to remove data.
CodePudding user response:
There's nothing built into Firebase to do this automatically, and it won't be possible to reliably do it with only code in your Android app.
The easiest way to do it would be in Cloud Functions, setting up a structure where you have both connections for the room under a single parent node:
room_presence: {
"room_1": {
"uid1": true,
"uid2": true
},
"room_2": {
"uid1": true,
"uid3": true
}
}
Whenever the last presence node gets delete from a room, that room itself also gets deleted automatically (as Firebase never stores a key without a value), so you can trigger a Cloud Function on the delete of that node and clean up Storage in there.