Skip to content

Feature Request: Include total in queryChannels request #1429

@Aidurber

Description

@Aidurber

First off I need to clarify that our usage of Stream is fairly non-standard. In our application we need to fetch all channels up front because we do channel categorisation. We cannot categorise on scroll and infinitely load that way because channels will jump around.

Our issue is that the response from client.queryChannels doesn't return a total. Because of this we need to request channels sequentially rather than in parallel. We see some requests for channels in the 3-5s range pretty frequently.

Here's the code we have to use:

        let allChannels: Channel[][] = []
        let offset = 0

        while (true) {
          const response = await client.queryChannels(filters, undefined, {
            limit: QUERY_LIMIT,
            offset,
            watch: true,
            state: true,
            message_limit: 50,
          })
          allChannels.push(response)
          if (response.length < QUERY_LIMIT) {
            break
          }
          offset += QUERY_LIMIT
        }

        return allChannels.flat()

If a coach has 100 users assigned to them so we need to make 4 requests due to the query limit being 30.
If Stream exposed a total: number in the API response we could request once to get the first page and the total, then fetch the rest in parallel which would improve the performance for us pretty drastically.

With the current way the API is designed we can sorta force parallel requests with something like this (naïve implementation):

         // Super rough and too static
        const ESTIMATED_MAX_CHANNELS = 300
        const requests = Math.ceil(ESTIMATED_MAX_CHANNELS / QUERY_LIMIT)
        const range = Array.from({ length: requests }, (_, i) => i * QUERY_LIMIT)

        const responses = await Promise.all(
          range.map((offset) =>
            client.queryChannels(filters, undefined, {
              limit: QUERY_LIMIT,
              offset,
              watch: true,
              state: true,
              message_limit: 50,
            }),
          ),
        )
        const data = responses.flat()

This could be improved by batching 100 channels at a time in parallel so we can overcome the ESTIMATED_MAX_CHANNELS limitation.

All of these are less efficient than the API telling us the total up front however and I'd rather not hit the Stream API more than we need to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions