Querying Members

The queryMembers endpoint allows you to list and paginate members for a channel. The endpoint supports filtering on numerous criteria to efficiently return member information. This endpoint is useful for channels that have large lists of members and you want to search members or if you want to display the full list of members for a channel.

Pagination and ordering

By default members are ordered from oldest to newest and can be paginated using offset-based pagination or by created_at or user_id fields.

Pagination by offset is the simplest to implement but it can lead to incorrect results if the list of members changes while you are paginating.

The recommended approach is to sort by “created_at” or by “user_id”.

let memberListController = client.memberListController(
  query: .init(
    cid: .init(type: .messaging, id: "general"),
    sort: [.init(key: .createdAt, isAscending: true)]
  )
)

memberListController.synchronize() { error in
  if error == nil {
    memberListController.members
  }
}

Stream Chat does not run MongoDB on the backend, only a subset of the query options are available.

Here’s some example of how you can query the list of members:

// > import StreamChat

// query by user.name
let controller = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .equal(.name, to: "tommaso"))
)

controller.synchronize { error in
  // handle error / access members
  print(error ?? controller.members)
}

// query members with name containing tom
let controller1 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .query(.name, text: "tom"))
)

controller1.synchronize { error in
  // handle error / access members
  print(error ?? controller1.members)
}

// autocomplete members by user name
let controller2 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .autocomplete(.name, text: "tom"))
)

controller2.synchronize { error in
  // handle error / access members
  print(error ?? controller2.members)
}

// query member by id
let controller3 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .equal(.id, to: "tommaso"))
)

controller3.synchronize { error in
  // handle error / access members
  print(error ?? controller3.members)
}

// query multiple members by id
let controller4 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .in(.id, values: ["tommaso", "thierry"]))
)

controller4.synchronize { error in
  // handle error / access members
  print(error ?? controller4.members)
}

// query channel moderators
let controller5 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .equal(.isModerator, to: true))
)

controller5.synchronize { error in
  // handle error / access members
  print(error ?? controller5.members)
}

// query for banned members in channel
let controller6 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .equal(.isBanned, to: true))
)

controller6.synchronize { error in
  // handle error / access members
  print(error ?? controller6.members)
}

// query members with pending invites
let controller7 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .equal("invite", to: "pending"))
)

controller7.synchronize { error in
  // handle error / access members
  print(error ?? controller7.members)
}

// query all the members
let controller8 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), filter: .none)
)

controller8.synchronize { error in
  // handle error / access members
  print(error ?? controller8.members)
}

// paginate channel members
controller8.loadNextMembers(limit: 10) { error in
  // handle error / access members
  print(error ?? controller8.members)
}

// order results by member created at descending
let controller9 = chatClient.memberListController(
  query: .init(cid: .init(type: .messaging, id: "general"), sort: [.init(key: .createdAt, isAscending: false)])
)

controller9.synchronize { error in
  // handle error / access members
  print(error ?? controller9.members)
}

Query Parameters

nametypedescriptiondefaultoptional
filtersobjectThe query filters to use. You can query on any of the custom fields defined above{}
sortobjectthe sort parameters{ created_at:1}
optionsobjectpagination options{ limit:100, offset:0}

By default when query members does not have any filter and it will match all members on your channel.

Member Queryable Built-In Fields

The following fields can be used to filter your query results

NameTypeDescriptionsupported operatorsExample
idstringthe id of the user$eq $intom
namestringthe name of the user$eq, $in, $autocomplete, $qTommaso
channel_rolestringthe member role$eqchannel_moderator
bannedbooleanthe banned status$eqfalse
invitestring, must be one of these values: (pending, accepted, rejected)the status of the invite$eqpending
joinedbooleanwhether member is joined the channel or not$eqtrue
created_atstring, must be formatted as an RFC3339 timestampthe time that the member was created$eq, $gt, $gte, $lt, $lte2021-01-15T09:30:20.45Z
updated_atstring, must be formatted as an RFC3339 timestampthe time the member was last updated$eq, $gt, $gte, $lt, $lte2021-01-15T09:30:20.45Z
last_activestring, must be formatted as an RFC3339 timestampthe time the user was last active$eq, $gt, $gte, $lt, $lte2021-01-15T09:30:20.45Z
cidstringthe cid of the channel that the user is a member of$eqmessaging:general
user.emailstringthe ‘email’ property of the user$eq, $in, $autcompleteuser@example.com

Also, you can pass any field available in the custom data.

Query Options

nametypedescriptiondefaultoptional
limitintegerThe number of members to return (max is 100)100
offsetintegerThe offset (max is 1000)0
user_id_ltstringPagination option: excludes members with ID greater or equal the value-
user_id_ltestringPagination option: excludes members with ID greater than the value-
user_id_gtstringPagination option: excludes members with ID less or equal the value-
user_id_gtestringPagination option: excludes members with ID less than the value-
created_at_afterstringPagination option: select members created after the date (RFC399)-
created_at_beforestringPagination option: select members created before the date (RFC399)-
created_at_before_or_equalstringPagination option: select members created before or equal the date (RFC399)-
created_at_after_or_equalstringPagination option: select members created after or equal the date (RFC399)-

Response

Field NameDescription
membersThe list of members matching the query
© Getstream.io, Inc. All Rights Reserved.