Schema Design for "unread" chat messages

3,470 views
Skip to first unread message

Eyal Ronel

unread,
Sep 10, 2014, 9:10:28 AM9/10/14
to fireba...@googlegroups.com, Zeev Glaizer
Hi Guys,

We're building our first app with firebase, and after reading the manuals, we're trying to figure out what would be the best way to store data for the following use case:

  1. we would like to create a instant messaging application where users can chat with one another, either one-on-one or within groups.
  2. We would like to split the scheme into 3 main "tables":
    1. users
    2. chats
    3. chat_messages
  3. Each user record will hold all active chats.
  4. Each chat will list a dictionary of all users taking part in it.
With this structure, what do you believe would be the best way to keep track of unread/unviewed messages by each user, for each of his/hers active chats?


Thanks and looking forward for you feedback,
Eyal

Tony Meng

unread,
Sep 10, 2014, 1:59:30 PM9/10/14
to fireba...@googlegroups.com, Zeev Glaizer
Eyal,

It depends on how you will be accessing that data. Given that you will most likely access the data with a specific key or a contiguous group of keys, you should ensure that you are able to index into your data properly when you access it. This will most likely result in creating unique paths used only for indexing given a particular access pattern. Don't be afraid to denormalize the data!

Here are a few examples of apps like yours, maybe you'll be able to see some similarities:

Here's a General Best Practices for Structuring Data link: https://www.firebase.com/docs/web/guide/structuring-data.html

Hope this helps!
Tony

--
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 post to this group, send email to fireba...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Hathaway

unread,
Sep 11, 2014, 12:00:04 PM9/11/14
to fireba...@googlegroups.com, ze...@priority.jobs

Eyal,
My first take would be to store a list of unread messages under each user, in addition to the list of all chats. So maybe something like:

users/userId/chats = [chatId1, chatId2, chatId3]
users/userId/unread = [{chatId1, chatSubject1, messageId3}, {chatId1, chatSubject,1, messageId4}, {chatId3, chatSubject3, messageId8}]

I'd definitely want to store the chat ID for each unread message even if it's available in the chat_message record already. Likewise, As Tony said, don't be afraid to denormalize. If you have other info (like possibly a subject line for the chat as I'm showing) you may want to include it here too. That way you can grab users/userId/unread to show a list of all chats with unread messages without having to separately load data from chats.

- John
Reply all
Reply to author
Forward
0 new messages