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,
completion: ((Swift.Result<(channels: [ChannelImpl], page: PubNubHashedPage?), Error>) -> Void)? = nil
)

Input

ParameterTypeRequiredDefaultDescription
filterStringNon/aExpression used to filter the results. Returns only these channels whose properties satisfy the given expression are returned. The filter 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
[ChannelImpl]Object containing a set of channels with pagination information.

Basic usage

Fetch all existing channel IDs.

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

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

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 string previously returned from the PubNub server.

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

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

/// Check total fetched channels
if totalFetchedChannels.count >= limit {
show all 45 lines

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 "chat" instance available
/// Function to fetch all archived (soft deleted) channels
func fetchArchivedChannels(chat: ChatImpl) {
var archivedChannels = [ChannelImpl]()

func fetchNextPage(page: PubNubHashedPage? = nil) {
/// Refer the "deleted" property on the channel object
let filter = "deleted == true"

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