#Updating a list or relationships in a document

64 messages · Page 1 of 1 (latest)

autumn notch
#

Hi guys. I don't know if this issue is serious enough for a support ticket.

I want to create a chat app and I need some advice. I have a chat collection that should have the chat ID, participants ID, and a list of messages.

At first, I thought about each message being a JSON encoded string. Later, I thought I could use relationships instead. But here is the thing - using either lists or relationships seem to have the same limitation. In Firebase, I could just set a document based on a given path and it can add the message to an existing list. But here in Appwrite, if I want to create a new message, I have to update the whole document each time, either I'm using lists or relationships.

Please what workarounds are there?

elfin rivet
#

What features does your chat app support? Group chat/Multiple participants? Other type of message content like media(images, videos,etc)? Quote feature?

How would your current document model/permission look like?

#

And what is the intention behind a json encoded string?

autumn notch
#

Just something similar to Twitter DMs

autumn notch
autumn notch
#

The JSON encoded string would contain the sender id, the sent time, attachment id, is read, the message, etc.

elfin rivet
#

1on1 chat. There can only be one convo between these two. So each chat have their own document collection. And message model could be as simple as String "message" and String "senderId". Appwrite will create $createdAt automatically for you, so you can use that to show the latest message

#

Create and read permissions for both participants and. And update or delete only for those who created the message

#

I think for other types of message you could use a prefix within the message.

<<TEXT>>Hey, how are you?
<<ATTACHMENT/IMAGE>>{{file of media from appwrite storage}}

#

If it is of type Text you just cut out the prefix and show the text

#

and for image you use the id to show the image

#

or you could add an enum and decide how to show the message based on that

#

I assume you know the id of the participant you chat with. You can use as collectionId:

  • Smaller userId starts first then append the userId of the second participant and use some fast hashing algorithm to create a hash out of that
autumn notch
elfin rivet
autumn notch
elfin rivet
#

Haven't considered that. Yeah then you can save it

autumn notch
#

In the last chat app I built with Realm, each chat object had a "participantsIDs" attribute. That way I could easily get a user's chats by querying the attribute using contains

#

But all in all, thanks for your help. I learnt a thing or two

#

I'd also like to know if there's a limit on the documents a collection could possibly hold

autumn notch
#

Alright.

#

Thank you

elfin rivet
#

And even if there would be one it wake take a long time for you to reach it lmao

#

I literally inserted over 50m documents to a collection to test its effiency

#

It all still works fine

autumn notch
#

I was wondering if to keep all the messages in a single collection or create a separate collection for each chat as you earlier mentioned

elfin rivet
#

I would recommend separate collection for each chat

#

Otherwise you would have to include a chatId to the message model as well

elfin rivet
#

In the first approach you only store the chatId once somewhere

autumn notch
#

I'm just tryna avoid having to write more cloud functions 😅

elfin rivet
#

but if each message contains chatId that can add up quite quickly

autumn notch
elfin rivet
#

Yeah and I think permissions are somewhat easier to maintain I think

autumn notch
#

Yeah. Thank you 🙏🏽

elfin rivet
#

For image attachments you could create a function:
Everytime a user uploads an image to a bucket where the chatId act as bucketId, a function gets triggered an creates a document in the chat collection with

message: <fileId>
type: image
senderId: <userId of sender>

autumn notch
#

I seem to have some doubts about this collection. I need to have a realtime subscription to the user's chats. If for instance, the user has 25 chats (which means 25 unique collections), does this mean that I will have to create a realtime subscription for the 25 collections? Isn't that too much?

elfin rivet
autumn notch
#

You mean it's possible to listen to multiple collection changes in one code block?

autumn notch
elfin rivet
#

If you have doubts, there is not really a downside to it

#

I mean you are interested in those 25 chats am I right?

autumn notch
elfin rivet
#

I use realtime extensivley and subscribe to a lot of channels and I personally do not feel any degradation because of that

autumn notch
#

Hmm. Alright then

#

Thanks for your help

#

One more question

#

Will the multiple channel sub work across different databases?

elfin rivet
#

Yes

autumn notch
#

Even without specifying the database ID?

elfin rivet
#

Yeah

#

That channel would just be called "documents" then

#

It will listen to all database events for which the user has permissions for

autumn notch
#

Alright

#

Thank you