Information Sources
When you register a listener for Firebase using on('child_added'
, it will be activated each time a new child is added.
ref.on('child_added', function(s) { console.log(s.key()); });
This listener will respond to all existing children as well as any future additions.
In the Firebase data provided:
hallo: gameSearching=false
sam_lous: gameSearching=true
test: gameSearching=false
test2: gameSearching=true
Queries Made
By registering a listener to a query, it will execute for each child that meets the query criteria:
ref.orderByChild("gameSearching").equalTo(true).on("child_added", function (s) { console.log(s.key()); })
This will display users searching for a game immediately and those who start later on.
sam_lous: gameSearching=true
test2: gameSearching=true
If you update a user's status to start looking for a game by calling
user.update({ gameSearching: true })
, this action will trigger the function. Essentially, you are presented with an updated list of users actively seeking games, managed automatically by Firebase.
For example, when user test
initiates a game search, upon setting gameSeaching
to true
, a child_added
event will occur:
test: gameSearching=true
Conversely, if user test stops their game search by executing
user.update({ gameSearching: false })
, a
child_removed
event will be triggered by Firebase.
Bounded Inquiries
Currently, three users remain in active game search mode:
sam_lous: gameSearching=true
test: gameSearching=true
test2: gameSearching=true
An additional query emerges featuring a limit:
ref.orderByChild("gameSearching").equalTo(true).limitToFirst(1).on("child_added", function (s) { console.log(s.key()); })
One child_added
event is generated for:
sam_lous: gameSearching=true
Upon matching sam_lous with an opponent and altering sam_lous' gameSearching
status to false, they no longer fit the query. Nevertheless, Firebase maintains query integrity by:
- Issuing a
child_removed
event for sam_lous
- Generating a
child_added
event for test
(the subsequent player in pursuit of a game)
Remember, Firebase functions not simply as a database querying tool, but as a dynamic data synchronization platform. Thus, your requests for synchronized information regarding game-seeking players are accurately fulfilled.