Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How firebase real time database reads data?

I am working on a chat app where messages store in firebase realtime database. Now if i make a node shown below ( chats-Between-A-and-B-Id are autogenerated chatroom keys ), so i want to know that when user S open chats with user T in chat app so Database will read only the messages stored in chats-Between-S-and-T-Id and will not read others chatroom messages !? am i right ? if yes then will it reduce the pricing?

chats-
      chats-Between-A-and-B-Id-
                 messageID1-…
                 messageID2-…
                 messageID3-…
                 ...
      chats-Between-o-and-p-Id-…
      chats-Between-s-and-t-Id-…
      chats-Between-x-and-y-Id-…

OR

If i store data like shown below then if user S open chats of user T in chat app so Database will read whole messages and

cahts-
      messageID1-…
      messageID2-…
      messageID3-…
      ...

sort them like (shown below) so it will read all messages and sort them and show in app to the user !? which will increase the pricing ?

if(chat.getReceiver() == senderID && chat.getSender() == receiverId ||
   chat.getReceiver() == receiverId && chat.getSender() == senderID)
                          {
                             (mChatList as ArrayList<Chat>).add(chat) //show chats
                          }

I want to know which approach can save my money.

Edit

I am using the 2nd approach (i got it from a chat app tutorial). Where we sort messages according to senders and receiver but after reading the answer of @sharath i get that if i go with this it will read every message from chat node and then sort them according to sender-reciver and if there are 1 million messages from all the users then it will read 1 million messages and sort only some of them only for 2 users! It will make me poor. So i am thinking about using the 1st approach where i will get messages from chat>>roomIdBetween2Users so it will ont read others chat and give me secure, maintainable and affordable databse.

And this is the image of my database here chats is parent node contains all the messages of all the users and sort them as i mentioned in the code for 2 users. There are already approximately 500+ messages in chats. And it will be so dangerous in future.enter image description here

like image 462
mr. groot Avatar asked Oct 15 '22 22:10

mr. groot


1 Answers

Its a little tricky situation to answer this without knowing your firebase DB structure. Am making an assumption and generalising this answer.

Firebase is a No SQL Database which stores data in tree structure or Parent child manner

Main important logic you should think of while developing anything with NoSQL databases are you need to create the Firebase DatabaseReference to the deepest node possible. Firebase queries and reads data which comes under the referenced node.

For example : if you have firebase structure as below

See this image of Firebase sample structure

If you want to access chats of User S, Create Reference to ID_User_S node.

note: If you create reference top node "Chats" then snapshot returns all child information under it

Regarding pricing : Yes the lesser the data you read lesser the price you pay. If you are referencing to deepest node possible you read lesser data. Try to optimise all firebase queries to reach to deepest node possible for creating a reference.

Now coming to your structure

chats-
      chats-Between-A-and-B-Id-
                 messageID1-…
                 messageID2-…
                 messageID3-…
                 ...
      chats-Between-o-and-p-Id-…
      chats-Between-s-and-t-Id-…
      chats-Between-x-and-y-Id-…

If you create a database reference to Chats node then each one trying to access the chat will read every child under it which includes others chats as well. So for efficient usage create reference to chats-Between-A-and-B-Id- so that you can get exact list of messages under those 2 users

like image 56
Sharath V Bhargav Avatar answered Oct 21 '22 02:10

Sharath V Bhargav