Querying/Checking users (100 of users) exists on my firebase database instance.

1,126 views
Skip to first unread message

joshi.vi...@gmail.com

unread,
Oct 26, 2015, 9:23:09 AM10/26/15
to Firebase Google Group
I have written a small chat android app, where in i authenticate the user with phone number and password. Phone number being the unique id for each user. When the user login to android app, i would like to present the list of contacts with whom the user can initiate the chat (similar to watsapp) and in order to do this i have to verify each and every contact is available on the chat platform (using firebase) or not.

As i understand, there are no where clause, contains and bulk query execution. 

Right now, i m firing almost close to 1000 (contact list size) firebase query to check contact exist on my chat platform, are there any better solution to handle this?

Kato Richardson

unread,
Oct 29, 2015, 6:02:32 PM10/29/15
to Firebase Google Group
Joshi,

Check out queries, which are similar to where clauses, and using indices to define complex relationships, which can provide any functionality not present in queries. The gist is thus: keep a separate list of contacts for the user and query that.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/dc2a8c76-d0f4-48c8-86f7-2f483b5a3dca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

joshi.vi...@gmail.com

unread,
Oct 30, 2015, 10:28:39 AM10/30/15
to Firebase Google Group
Thanks for the inputs.

I m not planning to store the contacts for the user in firebase. As mentioned earlier, each user would login with phone number and all users on this chat platform are identified by their phone number. Now, when i login to my app, i need the list of users from my contact list who are also available on my chat platform.

In SQL world, the query would look like:

SELECT * FROM <FIRBASE_USER_TABLE> WHERE USER_PHONE_ID IN ('0111111111','0222222222','1234567890');

One single query would give me the list of users that are part of my contact list who are also available on firebase chat platform. 

Right now, i m firing the below query to firebase for each phone number on my contact list. if the user has more than 1000 contacts, app will end up firing 1000 firebase query .
  1. Firebase ref = new Firebase("https://mychatplatform.xyz.com/users");
  2. Query queryRef = ref.orderByChild("user_phone_id").equalTo(0111111111);
Appreciate your help! 

Frank van Puffelen

unread,
Oct 30, 2015, 12:17:55 PM10/30/15
to Firebase Google Group
If you identify the users by phone number, consider storing them under a key that is their phone number.

That way you can do:

     ref.child(0111111111);

Direct access to the data beats querying for the same data.

You might want to consider requesting a brain wipe for the SQL knowledge. Trying to map SQL concepts to Firebase (and most other NoSQL solutions) is going to be a constant struggle and often lead to the wrong data architecture. In SQL you usually model the database for the way the data is, in NoSQL you more often model the data for the way you need to consume it.

     Frank

Jay

unread,
Oct 30, 2015, 1:42:38 PM10/30/15
to Firebase Google Group
What Frank said..  and a couple of other thoughts.

You should really consider disassociating your data from the keys you use to work with that data. I can't tell you how many times I had 'coded myself into a corner' by using a phone number or email as a key. The bottom line is that user data changes, it's dynamic. Keys don't (static). You are much better off creating a user node and levereging the uid for each user as it's key

users
  uid_0001
    name
    phone
  uid_0001
     name
     phone


that way the name and phone can be updated in just one place instead of going through lots of other notes updating all of them.

Second thought is to keep which users are available in a a 'connected' node

connected_users
    uid_0001
: true
    uid_0002
: true

when they connect, add their userId to the node and then set that to be removed on_disconnect.

That way,. when you app starts, it can simply observe that node to see who's online and additionally your app will get notified when a user goes offline.

Jay

unread,
Oct 30, 2015, 1:47:29 PM10/30/15
to Firebase Google Group
Typos - that was supposed to be

users
  uid_0001
    name
    phone
  uid_0002
     name
     phone


joshi.vi...@gmail.com

unread,
Nov 23, 2015, 5:52:41 AM11/23/15
to Firebase Google Group
Thank you guys for the input.
Reply all
Reply to author
Forward
0 new messages