[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
- Attributes Confusion
```import 'package:appwrite/models.dart'; class OrdersModel { String id, email, name, phone, status, user_id, address; int discount, total, created_at; L...
- I recently applied for the free plan und...
I recently applied for the free plan under the GitHub Student Developer Pack. However, my billing status still shows $15, and it mentions that this amount will ...
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...