How to Index query Firebase Realtime database with filter

143 views
Skip to first unread message

Pyae Phyo Kyaw

unread,
Aug 18, 2020, 9:52:05 AM8/18/20
to Firebase Google Group
         Hello, I'm creating an Android chat app using Firebase. Firstly, Sorry for my bad English writing skill. I have some problems and impossibilities with Firebase Realtime database structure.
        Scenario, in may chat app , there are 2 available chat options; single(1 to 1 ) chat and group chat. As documentations, I don't want users to fetch unnecessary data. So I structured my database like this.

chat:{
$UID1:{
$RoomID1 : 122113455,         // $UID1 last talk timestamp
$RoomID2 : 122113288,         //$UID1 last talk timestamp
...
},

$UID2:{
$RoomID1 : 121808790,          //$UID2 last talk timestamp
...
},
    ... 
    },

chat_info:{
$RoomID1:{
               last_talk: 1223345343, //Timestamp
              created_at: 1211424452,
          },

$RoomID2:{...},
...
    },

members:{
                    $RoomID1:{
                                          $UID1 : true,
                                          $UID2 : true,
                                          ...
                                       },
                    ...
        }


               As above JSON tree, a user can have many chat_rooms and a chat room can have many user. I want to retrieve all chat room of a user order by "chat_info/$RoomID/last_talk" where "last_talk" always change whenever new message is added. So if I add "last_talk" to "chat/$UID/", I have to change each user node every time new message is added in group chat. I search on google and there is no answer for cost and time effective approach. I think I need a new type of filter to this type of query. If this approach already exist or another database structure is suitable, kindly advice me . Thank you for your support.

Kato Richardson

unread,
Aug 19, 2020, 12:00:09 AM8/19/20
to Firebase Google Group
Hello Pyae,

It is simpler to just have the user listen to all the chat rooms they are a member of directly, so you only need one map of users to rooms. Here's an example.

☼, 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 view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/71f438d4-bc9f-43d1-b8d6-dec1b2492c38n%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Pyae Phyo Kyaw

unread,
Aug 19, 2020, 12:17:18 PM8/19/20
to Firebase Google Group
Hello Kato,

I read the code you have supported. And me ,as a mobile developer, I have small understanding about Typescript. I think your provided code first retrieve chat rooms under a unique userID and so what if user have 500 chat rooms which is bigger than the default size firebase allow us to retrieve once. And I can't find the timestamp sorting code in the provided snippet. Can you support me some idea? Thank you.

Kato Richardson

unread,
Aug 19, 2020, 1:13:11 PM8/19/20
to Firebase Google Group
I don't see a 500 limit in the Realtime Database limits doc. You should be able to listen on 500 chat rooms per user. If you optimize by only listening on the timestamp for each room, like my example, then it should scale well to many users.

I'm not sure what timestamp sorting code you mean. Do you have an example?

☼, Kato

On Wed, Aug 19, 2020 at 9:17 AM Pyae Phyo Kyaw <pyaeh...@gmail.com> wrote:
Hello Kato,

    I read the code you have supported. And me ,as a mobile developer, I have small understanding about Typescript. I think your provided code first retrieve chat rooms under a unique userID and so what if user have 500 chat rooms which is bigger than the default size firebase allow us to retrieve once. And I can't find the timestamp sorting code in the provided snippet. Can you support me some idea? Thank you.

--
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.

Pyae Phyo Kyaw

unread,
Aug 20, 2020, 4:54:06 AM8/20/20
to Firebase Google Group
Hello Kato,

The query limit is wrote here https://firebase.google.com/docs/database/rest/retrieve-data .

What does "If we set a limit of 100, we will only receive up to 100 matching children. If we have less than 100 messages stored in our database, we will receive every child. However, if we have over 100 messages, we will only receive data for 100 of those messages." in this document mean. May be I am misunderstanding or not.

And timestamp sorting code is not difficult in mobile . I don't worry about this. Thank you.

Kato Richardson

unread,
Aug 20, 2020, 12:42:38 PM8/20/20
to Firebase Google Group
That is a limit that you choose. So set it to whatever the limit is on how many chat rooms a user may join. I think being a member in more than 500 chat rooms is probably not productive (who could keep up with so many). So maybe that's a good limit?

☼, 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.

Kato Richardson

unread,
Aug 20, 2020, 12:43:09 PM8/20/20
to Firebase Google Group
Also, note that in my example, there wouldn't be a need to use a query or a limit. You could simply enforce that the user cannot list more than 500 rooms in their user/ record.
Reply all
Reply to author
Forward
0 new messages