1

I am trying to create a conversation-room node for my users. I have the node that has from and to users' ids. Before creating a new room, I need to check both cases, where 'from' is host and to is opponent' & 'to' is host and 'from' is opponent.

The node looks like:

 - Rooms
    - "id1"
      - from: 2
      - to: 1

What I want to do is basically:

Select all from rooms where from = 1 and to = 2 OR from = 2 and to = 1

Is there a way to check both cases in a single query? I got confused understanding the nature of complex querying data with Firebase.

senty
  • 12,385
  • 28
  • 130
  • 260
  • 1
    The simplest way to handle these 1:1 conversations is to name the room after the participating users. See http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase for an example. – Frank van Puffelen Jun 03 '16 at 03:22
  • Wow. That's brilliant. Thank you! With this way, would you suggest having combine 'rooms + conversations' and have only one table? or would it be bad for performance if I have messages and rooms in the same table? – senty Jun 03 '16 at 03:36
  • It's a way to have the same participants end up in the same chat room. Essentially rooms are just "named conversations". Or the other way around: conversations are really just "rooms where we use the uids as the room name". If a user is only looking at one room-or-conversation at a time, it's no performance problem. If that is the solution you go with, I'll mark this question as a duplicate btw. – Frank van Puffelen Jun 03 '16 at 03:50

1 Answers1

2

There is no direct way to query by two different children in firebase the only way to query by children would be:

 myRefToRooms.orderByChild('from').equalTo($user_id)

That said you can create in each room a query child that is created with the two uid:

 rooms:{
  $room_id:{
    from:$user_from,
    to:$user_to,
    query:$userfrom_$userto
  }
 }

This way you can get all the values that match both criteria

myRefToRooms.orderByChild('query').equalTo($user1_$user2)

Also you can use the startAt() and the endAt() methods in the query to not only find an exact match but all the rooms with the same "from user" or all the rooms with the same "to user"

If you don't like this approach you would need to structure your database in a different way to create simple queries

Ymmanuel
  • 2,523
  • 13
  • 12
  • `query:$userfrom_$userto` looks interesting but I didn't get how you achieve that? Do you combine them in a String and than explode? I was planning to create rooms in different node and conversations in different – senty Jun 03 '16 at 03:15
  • yep you combine them in a string, there are many ways to create the structure, and each has its own pro's and con's for example this method is only functional if the room will have a 1-1 relation between "to" and "from", can you add the structure you plan to use? – Ymmanuel Jun 03 '16 at 03:19
  • Frank's answer in comments section above seems brilliant approach for creating 1-1 structure I think. Thanks – senty Jun 03 '16 at 03:40
  • Yep seems like a great solution! – Ymmanuel Jun 03 '16 at 03:45
  • Your approach is also amazing approach. – senty Jun 03 '16 at 06:27