1

I'm developing an app on Android using Firebase that should embed a chat.

My issue is that to fetch messages, I need to query all the messages which have my uid either in sender or receiver field. This would be extremely easy to do in MySQL, but in Firebase (I must stick onto Firebase) looks kinda pain.

I cannot just filter them like that. And being receiver and sender fields of the object inside chat, I cannot even just filter them when I'm using a Firebase url like firebase.myapp.io/chat.

Firebase chat

So the only possible solution in this model, is fetching all the chats and client-filtering them. This is all but good way to do this job. Also, when the message will be many, if they should, everything could become extremely slow.

So I thought about different ways to achieve the result:

  • I get in chat the keys corresponding to user uid. Among the values, I get chats from user point of view, or I got the keys of my receivers as values, and inside the messages

But I don't like this very much since it can be extremely redundant, since every message should be inserted twice in the db.

Another way would be memorizing messages keys into another object, like chatmessages, and inside I got users uids as values, and as keys containing chat message keys.

What would be the best NoSQL way to manage multiple, private chat conversations?

Community
  • 1
  • 1
BlackBox
  • 631
  • 1
  • 5
  • 19
  • Duplicating data is quite common in NoSQL databases. See http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase for one way to model chat rooms. – Frank van Puffelen May 10 '16 at 13:28
  • So I should create objects like chat_uid1_uid2, and chat_uid2_uid1? Inside I should put the conversation right (message objects with time and text and eventually other stuff)? – BlackBox May 10 '16 at 14:57
  • I often find myself modeling in the database, what I see on the screen. So if you want personal chat rooms, I model personal chat rooms. Exactly what is best for you, depends on many factors that I don't know (and you likely don't know yet either). If you feel like reading more, I recommend [this article about NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/). – Frank van Puffelen May 10 '16 at 15:13

1 Answers1

0

I currently have the same problem with a chat application am creating. I recently found a solution:

1) I basically used the two users unique ID and converted them to a number using ID.getBytes().

2)Then I added the 2 numbers one to the other (addition) and converted them to a base64 number again.

3) Used the resulting number as the id for the chatroom.

It worked but since am doing all thisfor a android app it shows me a warning that am doing too much work on the main thread. Let me know if anyone knows a better way to do that!

BoyoApps
  • 1
  • 1