Back

[SOLVED] what's the suggested way to structure the DB for a chat function?

  • 0
  • Databases
  • General
  • Realtime
manazo
8 Nov, 2023, 15:46

I can create a separate collection for the chats only, and create a document for each message where the sender and receiver id with the message content will be stored. but i don't feel that's a really good idea when we think about realtime connections. cuz on each document added, the callback function for all the users will be executed. tho we can filter it with IDs, but that surely doesn't sound like the best approach, right?

So what approach would u suggest for a chat functionality?

TL;DR
The OP is asking for suggestions on how to structure a database for a chat function and is also seeking help with a non-working realtime connection. The OP shares some concerns about storing chat messages in one collection and the potential security flaw that arises when multiple users receive updates meant for someone else. Another user suggests creating a separate collection for chats and storing each message as a document with sender and receiver IDs. The OP is still unsure about the best approach and asks for further suggestions. Solution: One user suggests creating a separate collection for chats and storing each message as a document with sender and receiver IDs.
Haimantika
8 Nov, 2023, 18:55

Maybe this video can help - https://youtu.be/t7S0I78sloI

manazo
9 Nov, 2023, 06:49

in the video, he followed the same approach that i mentioned in my post. this one works for their scenario cuz they only have one chat room where all the users will chat. but my scenario is for a one user-to-user chat. like we do in whatsapp

ideclon
9 Nov, 2023, 10:19

I’m not sure what you mean by the “callback function”, but I would think the simplest way is to have one Collection for all messages, each message is added as a Document in that Collection, then you can use Realtime to display the message on the client side (if relevant) when a new Document is inserted. You could store all the details about the chat in another collection.

manazo
9 Nov, 2023, 16:26

according to the realtime doc, the syntax is

TypeScript
client.subscribe('account', response => {
    // Callback will be executed on all account events.
    console.log(response);
});

here the 2nd argument of the subscribe method takes a call back function where we get all the data. that's the callback i'm pointing to

now let's say my app has 4 users. The chat connection looks like below: User_1 <--> User_2 User_3 <--> User_4 So if we store all the messages as a doc in one collection, then while User_1 and User_2 chatting, User_3 and User_4 will also get those updates. we can check if the updates are actually for User_3 and User_4 and then show them those, but it's only can be done in the frontend, which is also a security flaw.

You could store all the details about the chat in another collection What u mean by storing all the details about the chat in another collection? can u explain it more? maybe with an example?

ideclon
9 Nov, 2023, 17:37

No, as long as you have permissions set correctly, only messages for the active user will come in

manazo
10 Nov, 2023, 09:23

yeah that's actually true. i wonder why i didn't thot it the first time..

manazo
10 Nov, 2023, 09:25

but speaking of realtime connections, would u mind checking why tf my realtime connection ain't working on this post ?

manazo
10 Nov, 2023, 09:25

[SOLVED] what's the suggested way to structure the DB for a chat function?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more