Querying Channels

Channel lists are a core part of most messaging applications, and our SDKs make them easy to build using the Channel List components. These lists are powered by the Query Channels API, which retrieves channels based on filter criteria, sorting options, and pagination settings.

Here’s an example of how you can query the list of channels:

const filter = { type: "messaging", members: { $in: ["thierry"] } };
const sort = [{ last_message_at: -1 }];
const options = { limit: 15 };

const channels = await chatClient.queryChannels(filter, sort, options);

Query Parameters

NameTypeDescriptionDefaultOptional
filtersobjectFilter criteria for channel fields. See Queryable Fields for available options.{}
sortobject or array of objectsSorting criteria based on field and direction. You can sort by last_updated, last_message_at, updated_at, created_at, member_count, unread_count, or has_unread. Direction can be ascending (1) or descending (-1). Multiple sort options can be provided.[{last_updated: -1}]
optionsobjectAdditional query options. See Query Options.{}

An empty filter matches all channels in your application. In production, always include at least members: { $in: [userID] } to return only channels the user belongs to.

The API only returns channels that the user has permission to read. For messaging channels, this typically means the user must be a member. Include appropriate filters to match your channel type’s permission model.

Common Filters

Messaging and Team Channels

For most messaging applications, filter by channel type and membership:

const filter = { members: { $in: ["thierry"] }, type: "messaging" };

Filtering by Tags

Use filter_tags to categorize and query channels by topic, priority, department, or any custom categorization.

The filter_tags field supports two operators:

  • $in: Returns channels that have any of the specified tags
  • $eq: Returns channels that have exactly the specified tags (no more, no less)
// Find channels with any of the specified tags
const filter = { filter_tags: { $in: ["premium", "vip"] } };

// Find channels with exactly these tags
const exactFilter = { filter_tags: { $eq: ["support", "urgent"] } };

const channels = await chatClient.queryChannels(filter, sort);

Example use cases for filter tags:

  • Support Tiers: ["premium", "standard", "basic"]
  • Departments: ["sales", "support", "billing"]
  • Priority Levels: ["urgent", "high", "normal", "low"]
  • Topics: ["technical", "account", "feature-request"]
  • Status: ["active", "archived", "pending"]

Combining tags with other filters:

// Find urgent channels assigned to the current agent
const filter = {
  filter_tags: { $in: ["urgent"] },
  agent_id: currentAgent.id,
  type: "messaging",
};

const urgentChannels = await chatClient.queryChannels(filter, [
  { last_message_at: -1 },
]);

Limits: Channels can have up to 10 tags, with each tag limited to 255 characters. Tags are automatically sorted and deduplicated.

Channel Queryable Built-In Fields

The following fields can be used in your filter criteria:

NameTypeDescriptionSupported OperatorsExample
frozenbooleanChannel frozen status$eqfalse
typestring or list of stringChannel type$in, $eqmessaging
idstring or list of stringChannel ID$in, $eqgeneral
cidstring or list of stringFull channel ID (type:id)$in, $eqmessaging:general
membersstring or list of stringUser IDs of channel members$in, $eqmarcelo or [thierry, marcelo]
invitestring (pending, accepted, rejected)Invite status$eqpending
joinedbooleanWhether the current user has joined the channel$eqtrue
mutedbooleanWhether the current user has muted the channel$eqtrue
member.user.namestringName property of a channel member$autocomplete, $eqmarc
created_by_idstringID of the user who created the channel$eqmarcelo
hiddenbooleanWhether the current user has hidden the channel$eqfalse
last_message_atstring (RFC3339 timestamp)Time of the last message$eq, $gt, $lt, $gte, $lte, $exists2021-01-15T09:30:20.45Z
member_countintegerNumber of members$eq, $gt, $lt, $gte, $lte5
created_atstring (RFC3339 timestamp)Channel creation time$eq, $gt, $lt, $gte, $lte, $exists2021-01-15T09:30:20.45Z
updated_atstring (RFC3339 timestamp)Channel update time$eq, $gt, $lt, $gte, $lte2021-01-15T09:30:20.45Z
teamstringTeam associated with the channel$eqstream
last_updatedstring (RFC3339 timestamp)Time of last message, or channel creation time if no messages exist$eq, $gt, $lt, $gte, $lte2021-01-15T09:30:20.45Z
disabledbooleanChannel disabled status$eqfalse
has_unreadbooleanWhether the user has unread messages (only true supported, max 100 channels)truetrue
app_bannedstringFilter by application-banned users (only for 2-member channels)excluded, onlyexcluded
filter_tagslist of stringsTags associated with the channel$in, $eq[premium, free]

For best performance, use cid (full channel ID) instead of id when querying by channel identifier. The cid field is indexed for faster lookups.

For supported query operators, see Query Syntax Operators.

The app_banned filter only works on direct message channels with exactly 2 members.

Query Options

NameTypeDescriptionDefaultOptional
statebooleanReturn channel statetrue
watchbooleanSubscribe to real-time updates for returned channelstrue
limitintegerNumber of channels to return (max 30)10
offsetintegerNumber of channels to skip (max 1000)0
message_limitintegerMessages to include per channel (max 300)25
member_limitintegerMembers to include per channel (max 100)100

Response

The API returns a list of ChannelState objects containing all information needed to render channels without additional API calls.

ChannelState Fields

Field NameDescription
channelChannel data
messagesRecent messages (based on message_limit)
watcher_countNumber of users currently watching
readRead state for up to 100 members, ordered by most recently added (current user’s read state is always included)
membersUp to 100 members, ordered by most recently added
pinned_messagesUp to 10 most recent pinned messages

Pagination

Use limit and offset to paginate through results:

const filter = { members: { $in: ["thierry"] } };
const sort = { last_message_at: -1 };

// Get channels 11-30
const channels = await authClient.queryChannels(filter, sort, {
  limit: 20,
  offset: 10,
});

Always include members: { $in: [userID] } in your filter to ensure consistent pagination results. Without this filter, channel list changes may cause pagination issues.

Best Practices

Channel Creation and Watching

A channel is not created in the API until one of the following methods is called. Each method has subtle differences:

channel.create();
channel.query();
channel.watch();

Only one of these is necessary. For example, calling watch automatically creates the channel in addition to subscribing to real-time updates—there’s no need to call create separately.

With queryChannels, a user can watch up to 30 channels in a single API call. This eliminates the need to watch channels individually using channel.watch() after querying. Using queryChannels can substantially decrease API calls, reducing network traffic and improving performance when working with many channels.

Filter Best Practices

Channel lists often form the backbone of the chat experience and are typically one of the first views users see. Use the most selective filter possible:

  • Filter by CID is the most performant query you can use
  • For social messaging (DMs, group chats), use at minimum type and members: { $in: [userID] }
  • Avoid overly complex queries with more than one AND or OR statement
  • Use custom fields on channels to simplify filters
  • Filtering by type alone is not recommended—always include additional criteria
// Most performant: Filter by CID
const filter = { cid: channelCID };

// Recommended for social messaging
const filter = { type: "messaging", members: { $in: [userID] } };

// Not recommended: type alone
const filter = { type: "messaging" };

If your filter returns more than 50% of channels in the application, consider adding more selective criteria. Contact support if you plan on having millions of channels and need guidance on optimal filters.

Sort Best Practices

Always specify a sort parameter in your query. The default is last_updated (the more recent of created_at and last_message_at).

The most optimized sort options are:

  • last_updated (default)
  • last_message_at
const sort = { last_message_at: -1 };

Sorting on custom fields is possible but has scalability implications—it performs well up to approximately 2,000-3,000 channels. Enterprise customers can contact support for optimization options.

For the full list of supported query operators, see Query Syntax Operators.