0

I'm building a basic kind of whatsapp functionality for an app and I need to be able to query firebase for messages that match user1 and user2's IDs as noted below in receiver and sender.

I'm using Ionic 2 and I have both firebase and angularfire 2 libraries imported into the app already I am having a bit of trouble how I can pull all messages that contain both user1 and user2 but no other messages at all.

This is kind of what I am looking for but theres nothing in the docs that would support this kind of advanced query or possibly I have missed it. Thinking out loud its going to have to be some kind of correlated subquery or something like left joins on the same table (if it was mysql)

this.items2 = af.database.list('/messages', {
    query: {
      orderByChild_1: 'sender',
      equalTo: user1 || user2,
      and
      orderByChild_2: 'receiver',
      equalTo: user1 || user2,          
    }
  });

here is the database structure:

 messages:
    - Kc8da64IZjqv8PSsAGI
        message: "Hi How are you"
        receiver: "XRgPwmaPJWWIF8xLzvuQ28j5nKe2"
        sender: "ddL5H70N3hYW8n4WpDrhNfm32Fc2"

    - dkdwrehjdsfhaseuyr89
        message: "Im great and you ?"
        receiver: "ddL5H70N3hYW8n4WpDrhNfm32Fc2"
        sender: "XRgPwmaPJWWIF8xLzvuQ28j5nKe2"

    - i3uer8ufdisjhdsiflhlsdkhf
        message: "pretty good, where are you ?"
        receiver: "XRgPwmaPJWWIF8xLzvuQ28j5nKe2"
        sender: "ddL5H70N3hYW8n4WpDrhNfm32Fc2"

    - 23r9eyfdusikljnmsfjlskdjfklsjdfkl
        message: "Im in cuba"
        receiver: "XRgPwmaPJWWIF8xLzvuQ28j5nKe2"
        sender: "ddL5H70N3hYW8n4WpDrhNfm32Fc2"    
Community
  • 1
  • 1
Hamburgersn Heroin
  • 207
  • 1
  • 2
  • 12
  • Model chat rooms. Don't just keep one long list of messages between all users, but instead keep the messages between two (or any group of) users in a separate node from the others. That prevents the need for these complex (and expensive) queries. See my answer here http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase, read [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) and view [Firebase for SQL developers](https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s). – Frank van Puffelen Feb 04 '17 at 16:12
  • That is very cool and solves some issues ive had in the back of my head whilst trying to figure out this other stuff.. thanks, btw I just read some other answers of your on this topic which are gonna help me out... dont suppose you have any other tricks that I could look at related to this – Hamburgersn Heroin Feb 04 '17 at 16:20
  • The three links I provided should give you a good basic understanding of NoSQL in general and Firebase specifically. – Frank van Puffelen Feb 04 '17 at 16:24
  • Sweet, been reading over them. very helpful. but when you want to list all the message threads pertaining to one user.. what would you recommend for that ? for instance I have a thread between 2 people with this id now: chat_XRgPwmaPJWWIF8xLzvuQ28j5nKe2_ddL5H70N3hYW8n4WpDrhNfm32Fc2 --- so how do I display all the messages that contain a given users id within that string ? – Hamburgersn Heroin Feb 04 '17 at 16:53
  • .. doesnt seem to cover that kind of scenario in the posts :) – Hamburgersn Heroin Feb 04 '17 at 16:59
  • Stop thinking of "I need to query this huge list of items to get these 3 things from it". Instead start modeling your data for the use-case. "I need to list the rooms that a user is in, so I should store the rooms/room ids for each user". `/userRooms/$uid: { room1: true, room2: true, room3: true }` – Frank van Puffelen Feb 04 '17 at 17:11
  • Sorry, I have still got an sql head - the only way I can see to do such a thing is to setup another collection/table that has each users ID and under that each string for the rooms like "chat_XRgPwmaPJWWIF8xLzvuQ28j5nKe2_ddL5H70N3hYW8n4WpDrhNfm32F‌​c2" using the example you linked to for creating the "room" name I cant see any other way to do it other than this but i guess this is wrong because its very sql like ? – Hamburgersn Heroin Feb 04 '17 at 17:18
  • What you're describing is the same as the example I gave in my last comment, except that I simplified the room names there. – Frank van Puffelen Feb 04 '17 at 17:46
  • Cool.. I had already started to build it out that way :) – Hamburgersn Heroin Feb 04 '17 at 18:09

0 Answers0