Best Way to Manage Chat Channels in Firebase

Best way to manage Chat channels in Firebase

A common way to handle such 1:1 chat rooms is to generate the room URL based on the user ids. As you already mention, a problem with this is that either user can initiate the chat and in both cases they should end up in the same room.

You can solve this by ordering the user ids lexicographically in the compound key. For example with user names, instead of ids:

var user1 = "Frank"; // UID of user 1
var user2 = "Eusthace"; // UID of user 2

var roomName = 'chat_'+(user1<user2 ? user1+'_'+user2 : user2+'_'+user1);

console.log(user1+', '+user2+' => '+ roomName);

user1 = "Eusthace";
user2 = "Frank";

var roomName = 'chat_'+(user1<user2 ? user1+'_'+user2 : user2+'_'+user1);

console.log(user1+', '+user2+' => '+ roomName);
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

Direct messages chat with firebase - data structure?

For anyone who's interested. The solution was pretty simple, well as always :)

I used a mix of both userid's as key of a conversation. And in order to know, which one goes first, i simply ordered them, so they are always arranged the same way.

Example:
userid1: abcde
userid2: zudfg

so the conversation id would be: abcdezudfg

That makes it pretty easy to access it or even check weather a conversation between these users already exist or not.

I hope this helps.

Best way of storing chats in JSON tree databse?

When it comes to the Firebase Database (and most NoSQL data stores), it's often best to flatten your data.

Users
|
|_USER1
| |
| |__FRIENDS
|
|_USER2
|
|__FRIENDS
UserChats
|
|_USER1
| |
| |__chat w/USER2
|
|_USER2
|
|__chat w/USER1

This way you can look up the user's friend list without having to load their list of chats.

Also look at this answer about a convenient scheme for constructing 1:1 chat room identifiers: Best way to manage Chat channels in Firebase

How to structure Firestore database in chat app?

What you've offered as the first option is close to how you'd model this in a relation database in a tblMessages. Such a literal translation is seldom your best option in NoSQL databases, since they have very different trade-offs. For example, you've already noticed that you need to perform a query on two fields to get the messages between two users.

When modeling data on a NoSQL database, I usually recommend modeling in your database for what you see on your screen. So if your chat app has the concept of chat rooms (i.e. persistent chats between specific groups of people), I'd model those in your database too.

In Cloud Firestore that means that you'd have a top-level collection with a document for each chat room, and then a subcollection under each such document with the messages for that chat room:

ChatRooms (collection)
ChatRoom1 (document)
Messages (collection)
Message1_1 (document)
Message1_2 (document)
ChatRoom2 (document)
Messages (collection)
Message2_1 (document)
Message2_2 (document)

With this model you don't need to query to show the messages in a chat room, but can instead just load (all) the messages straight from the subcollection for that room. It also has the advantage that you partition the rooms, meaning that writes can scale much better.

I typically recommend modeling the chat room document IDs after the participants in the room, so that you can easily reconstruct the ID based on the participants. But there are more valid options for this.



Related Topics



Leave a reply



Submit