The QueryChannels API is typically core to most chat applications because it provides the data to populate a list of channels the user can interact with. The API accepts two parameters filter and sort
We don't restrict the size of the filter you provide with the API. We also allow a variety of operators to be applied to reserved fields and custom data, making it possible to build increasingly complex query filters.
However, what may work with small data sets in development may not work well once your application has been populated with millions of records. This is because the database controllers have to filter through increasingly large sets of data with complex criteria, and eventually, they will time out with a 500 response.
Keep your Query Filter Simple
We always advise keeping the filter as simple as possible. When you do have a more complicated filter with operators such as OR or AND and custom data, we recommend asking the support team if it will scale. We can also consider adding indexes to our DB on your custom fields to speed up the request.
Don't use Stream Channels as a Database
One antipattern we see increasingly often is using the Stream Channels system as a database rather than your own service.
For example, imagine I run a Dating App, and I would like to populate a page of conversations for a user with people whom they have matched with, are less than 1 week old, and have a mutual interest, or the user has super-matched. You may design a filter such as
const filter = {
$or: [
{
members: { $in: [userId] },
type: "messaging",
created_at: { $lte: timeStampOneWeekAgo },
mutual_match: true
},
{
super_match: true
}
]
}
However, due to the amount of custom data and OR operator, this query will be slow and may even timeout, resulting in a bad user experience.
What is preferred is that you store this data on your service and then use Stream Chat to fetch corresponding channels by storing the CID of the channels in your service. That way, the query can be performant
const filter = {cid: arrayOfChannelCIDs}
Comments
0 comments
Please sign in to leave a comment.