List all channels
getChannels()
returns a paginated list of all existing channels.
By default, this method returns all custom channel metadata without the need to explicitly define that during the call.
Requires App Context
To store data about channels, you must enable App Context for your app's keyset in the Admin Portal.
To get a list of all channels a user is a member of, use the getMemberships()
method.
Method signature
This method takes the following parameters:
chat.getChannels(
filter: String? = nil,
sort: [PubNub.ObjectSortField] = [],
limit: Int? = nil,
page: PubNubHashedPage? = nil,
) async throws -> (channels: [ChannelImpl], page: PubNubHashedPage?)
Input
Parameter | Description |
---|---|
filter Type: String Default: n/a | Expression used to filter the results. Returns only these channels whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: [PubNub.ObjectSortField] Default: [] | 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. |
limit Type: Int Default: nil | Number of objects to return in response. The default (and maximum) value is 100 . |
page Type: PubNubHashedPage? Default: nil | Object used for pagination to define which previous or next result page you want to fetch. |
Output
Parameter | Description |
---|---|
(channels: [ChannelImpl], page: PubNubHashedPage?) | A tuple containing a set of channels and pagination information indicating the start, end, and total count of the members. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Fetches channel items in a single request.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
// Fetch the first set of channels.
// There's no need to pass the "page" parameter, as it's nil by default, indicating the first page
let getChannelsResult = try await chat.getChannels()
debugPrint("Fetched channels: \(getChannelsResult.channels)")
debugPrint("Next page: \(String(describing: getChannelsResult.page))")
// The example below shows how to read the next page and pass it to a subsequent call.
// You can recursively keep fetching using the page objects from previous responses until the returned array is empty
if let nextPage = getChannelsResult.page, !getChannelsResult.channels.isEmpty {
let resultsFromNextPage = try await chat.getChannels(page: nextPage)
}
}
Other examples
Pagination
Get the number of 25 channels and then specify that you want to fetch the results from the next page using a page previously returned from the PubNub server.
// Assuming you have a reference of type "ChatImpl" named "chat" and the "page" reference of type "PubNubHashedPage"
Task {
let getChannelsResult = try await chat.getChannels(limit: 25, page: page)
debugPrint("Fetched channels: \(getChannelsResult.channels)")
debugPrint("Next page: \(String(describing: getChannelsResult.page))")
// The example below shows how to read the next page and pass it to a subsequent call.
// You can recursively keep fetching using the page objects from previous responses until the returned array is empty
if let nextPage = getChannelsResult.page, !getChannelsResult.channels.isEmpty {
let resultsFromNextPage = try await chat.getChannels(limit: 25, page: nextPage)
}
}
Archived channels
Get all archived channels. This request will return all channels removed with the soft
option set to true
, whose data is still stored in the App Context storage.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
let getChannelsResult = try await chat.getChannels(filter: "status == 'deleted'", limit: 25, page: page)
debugPrint("Fetched channels: \(getChannelsResult.channels)")
debugPrint("Next page: \(String(describing: getChannelsResult.page))")
// The example below shows how to read the next page and pass it to a subsequent call.
// You can recursively keep fetching using the page objects from previous responses until the returned array is empty
if let nextPage = getChannelsResult.page, !getChannelsResult.channels.isEmpty {
let resultsFromNextPage = try await chat.getChannels(page: nextPage)
}
}