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,
sort?: object,
limit?: number,
page?: {
next?: string,
prev?: string
}
}): Promise<{
channels: Channel[],
page: {
next: string,
prev: string,
},
total: number,
show all 16 linesInput
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
filter | string | No | 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 | object | No | n/a | Key-value pair of a property to sort by, and a sort direction. 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"} . By default, the items are sorted by the last updated date. |
limit | number | No | 100 | Number of objects to return in response. The default (and maximum) value is 100 . |
page | object | No | n/a | Object used for pagination to define which previous or next result page you want to fetch. |
→ next | string | No | n/a | Random string returned from the server that can be used as a cursor bookmark for fetching the next result page. |
→ prev | string | No | n/a | Random string returned from the server that can be used as a cursor bookmark for fetching the previous result page. Ignored if you also supply the next parameter. |
Output
Parameter | Type | Description |
---|---|---|
Promise<> | object | Returned object containing three fields: channels , page , and total . |
→ channels | Channel[] | List of all matching channels. |
→ page | object | Object that lets you either fetch the next (next ) or previous (prev ) result page. |
→ next | string | Random string returned from the server that can be used as a cursor bookmark for fetching the next result page. |
→ prev | string | Random string returned from the server that can be used as a cursor bookmark for fetching the previous result page. Ignored if you also supply the next parameter. |
→ total | number | Total number of Channel objects matching the request query. |
Basic usage
Fetch all existing channel IDs.
// reference the "chat" object and invoke the "getChannels()" method
const channels = await chat.getChannels()
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.
// result page 1
const channelsObject = await chat.getChannels(
{
limit: 25,
}
)
// result page 2
const channelsObjectPage2 = await chat.getChannels(
{
limit: 25,
page: { next: channelsObject.page.next }
}
)
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.
const channels = await chat.getChannels(
{
filter: "status=='deleted'"
}
)