List all users

Get a paginated list of all users and their details using the getUsers() method.

By default, this method returns all custom user metadata without the need to define that during the call explicitly.

Requires App Context

To store data about users, you must enable App Context for your app's keyset in the Admin Portal.

Method signature

This method takes the following parameters:

chat.getUsers(
filter: String? = nil,
sort: [PubNub.ObjectSortField] = [],
limit: Int?,
page: PubNubHashedPage? = nil,
completion: ((Swift.Result<(users: [UserImpl], page: PubNubHashedPage?), Error>) -> Void)? = nil
)

Input

ParameterTypeRequiredDefaultDescription
filterStringNon/aExpression used to filter the results. Returns only these users whose properties satisfy the given expression are returned. The filtering language is defined here.
sort[PubNub.ObjectSortField]No[]A collection to specify the sort order. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. Unless specified otherwise, the items are sorted by the last updated date. Defaults to an empty list.
limitIntNo100Number of objects to return in response. The default (and maximum) value is 100.
pagePubNubHashedPageNon/aObject used for pagination to define which previous or next result page you want to fetch.

Output

TypeDescription
[UserImpl]Object containing a set of users with pagination information (next, prev, total).

Basic usage

Fetch all existing user IDs.

// Assuming you have a "chat" instance available
// Function to fetch the users and handle pagination
func fetchUsers(chat: ChatImpl, page: PubNubHashedPage? = nil) {
chat.getUsers(limit: 100, page: page) {
switch $0 {
case let .success((users, nextPage)):
// Process fetched user IDs
let userIDs = users.map { $0.id }
debugPrint("Fetched user IDs: \(userIDs)")

// Fetch the next page if available
if let next = nextPage {
fetchUsers(chat: chat, page: next)
} else {
debugPrint("All user IDs fetched.")
show all 27 lines

Other examples

Pagination

Get the total number of 25 users and then specify that you want to fetch the results from the next page using a string that was previously returned from the PubNub server.

// Assuming you have a "chat" instance available
/// Function to fetch a specified number of users and handle pagination using a provided cursor
func fetchUsers(chat: ChatImpl, limit: Int = 25, startCursor: String? = nil) {
var totalFetchedUsers = [UserImpl]()

func fetchNextPage(page: PubNubHashedPage? = nil) {
chat.getUsers(limit: limit, page: page) {
switch $0 {
case let .success((users, nextPage)):
// Accumulate fetched users
totalFetchedUsers.append(contentsOf: users)
debugPrint("Fetched user IDs: \(users.map { $0.id })")

// Check total fetched users
if totalFetchedUsers.count >= limit {
show all 45 lines

Archived users

Get all archived users. This request will return all users removed with the soft option set to true, whose data is still stored in the App Context storage.

// Assuming you have a "chat" instance available
/// Function to fetch all archived (soft deleted) users
func fetchArchivedUsers(chat: ChatImpl) {
var archivedUsers = [UserImpl]()

func fetchNextPage(page: PubNubHashedPage? = nil) {
// Assuming "status" is the field name and "deleted" is the value for soft deleted users
let filter = "status == 'deleted'"

chat.getUsers(filter: filter, limit: 100, page: page) {
switch $0 {
case let .success((users, nextPage)):
// Accumulate fetched archived users
archivedUsers.append(contentsOf: users)
debugPrint("Fetched archived user IDs: \(users.map { $0.id })")
show all 36 lines
Last updated on