I am writing a polling app with a Firebase backend. Users answer to yes/no questions and see results. They can't see who answered the question though, only their friends can.
questions
question1:
readable
text: "Do you enjoy Stack Overflow?"
yes_count: 76
no_count: 14
response:
user1: yes
user2: no
...
user1000: no
users:
user1:
friends:
user1
user999
One way to do this is to call onChildAdded
on the response
node and do filtering on the client side. This may result in downloading thousands of unnecessary UIDs.
Another way is to ask the server to do it. Users put requests to get friends' UIDs on a queue and the backend does the filtering and puts the result at a location that users can listen to. But this is a big extra load, especially considering that whenever there is a new answer to a question the backend has to check for every user whether that new answer is from a friend.
How do I query Firebase to get only friends' UIDs, instead of everyone's, at the response
node?
If this is not possible, is it better for users to do filtering (and downloading of thousands of unneeded UIDs), or for the server to do it (huge extra load)?
This becomes trivial once you require that friendships are bi-directional, so also store the reverse relationship. E.g.
friends:
user1:
user42
user999
user42:
user1
user999:
user1
With this bidirectional data structure, if a user posts you can simply load their friend list with /friends/$uid
and loop over that. No expensive query needed.