The queryChannels call is used to generate a list of channels that fit certain criteria, such as a specific channel type, or all channels that a user is a member of.
The maximum amount of channels this method can return is 30. This is in place to prevent overloading our servers.
Paginating through all channels is possible by using our limit and offset parameters, provided to the third "options" argument of the method.
This is a boilerplate function that is a good starting point for pagination. In most cases, it is best to include another condition in addition to the while loop that makes the loop only continue when the user has hit the bottom of the scroll area, or presses a 'load more' button. If this is being run server-side (to generate a list of channels to export, for example), then you will just need to be aware of your rate limits, and implement a backoff-retry mechanism if necessary to avoid hitting your rate limit.
Warning: Running this function without any logic to trigger it on certain user events will cause it to run until all channels are returned, this will increase your chances of hitting your rate limits.
For further assistance, feel free to contact support with any questions.
const getChannels = async () => {
let offset = 0;
let lastFetch = 30;
let channels = [];
const filter = { type: 'messaging' };
const sort = [{ last_message_at: -1 }];
const options = {limit: 30, offset: 0}
while (lastFetch === 30) {
await serverClient.queryChannels(filter, sort, options).then(r => {
options.offset = options.offset+30;
lastFetch = r.length;
channels = ([...channels, ...r])
});
}
return channels;
}
Comments
0 comments
Please sign in to leave a comment.