[SOLVED] what's the suggested way to structure the DB for a chat function?
- 0
- Databases
- General
- Realtime

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?

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

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

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.

according to the realtime doc, the syntax is
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?

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

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

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

[SOLVED] what's the suggested way to structure the DB for a chat function?
Recommended threads
- Existing ID bug?!
Idk why this happens can someone explain!! why Im getting this error even if I "manually clear the collection" ?
- Constant 404 Pages
Whenever I am navigating through items and pages to go through my collections and database I end up at a page that sends me a 404 error, if i refresh it remains...
- Getting [object] [object] from appwrite
I'm fetching the data from appwrite having but for some reason I'm getting data in [object] [object] format why?
