Channel groups
Channel groups let you subscribe to multiple channels with a single subscription. Add channels to a group and receive messages from all of them at once.
Use channel groups to:
- Subscribe to all user channels with one call
- Monitor presence across multiple channels
- Manage subscriptions dynamically without reconnecting
Publishing to a channel group is not supported. Publish to each channel individually.
Requires Stream Controller
Enable Stream Controller for your key in the Admin Portal. See the support page for details.
Get channel group reference
Get a reference to a channel group with getChannelGroup(). This returns a handle to manage the group without creating it on the server.
If the group exists, the reference points to it. Otherwise, the handle creates the group when you add channels.
Method signature
This method takes the following parameters:
1chat.getChannelGroup(channelGroupId: String) -> ChannelGroupImpl
Input
| Parameter | Description |
|---|---|
channelGroupId *Type: StringDefault: n/a | Unique identifier for the channel group. |
Output
| Type | Description |
|---|---|
ChannelGroupImpl | A ChannelGroup object you can use to manage the group. |
Sample code
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.
Get a reference to a channel group named my-channel-group.
1
Remove channel group
Delete a channel group from the server with removeChannelGroup().
Method signature
This method takes the following parameters:
1chat.removeChannelGroup(id: String) async throws
Input
| Parameter | Description |
|---|---|
id *Type: StringDefault: n/a | Unique identifier of the channel group to remove. |
Output
This method doesn't return any value.
Sample code
Remove a channel group named my-channel-group.
1
List channels
Get a paginated list of all channels in a channel group with listChannels().
Method signature
This method takes the following parameters:
1channelGroup.listChannels(
2 filter: String? = nil,
3 sort: [PubNub.ObjectSortField] = [],
4 limit: Int? = nil,
5 page: PubNubHashedPage? = nil
6) async throws -> (channels: [ChannelImpl], page: PubNubHashedPage?, total: Int?)
Input
| Parameter | Description |
|---|---|
filterType: StringDefault: nil | Expression used to filter the results. Returns only the channels whose properties satisfy the given expression. The filter language is defined here. |
sortType: [PubNub.ObjectSortField]Default: [] | An array to specify the sort order. Available options are id, name, and updated. Use ascending or descending to specify the sorting direction. |
limitType: IntDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100. |
pageType: PubNubHashedPageDefault: nil | Object used for pagination to define which previous or next result page you want to fetch. |
Output
| Type | Description |
|---|---|
(channels: [ChannelImpl], page: PubNubHashedPage?, total: Int?) | A tuple containing a set of channels with pagination information. |
Sample code
List all channels in a channel group.
1
Add channels
Add Channel entities to a channel group with add(channels:).
Method signature
This method takes the following parameters:
1channelGroup.add(channels: [ChannelImpl]) async throws
Input
| Parameter | Description |
|---|---|
channels *Type: [ChannelImpl]Default: n/a | Array of Channel entities to add to the group. |
Output
This method doesn't return any value.
Sample code
Add two channels to a channel group.
1
Add channel identifiers
Add channels to a group by ID with addChannelIdentifiers(_:). This avoids fetching full Channel entities.
No validation
This method does not validate that the channel IDs exist.
Method signature
This method takes the following parameters:
1channelGroup.addChannelIdentifiers(_ channelIds: [String]) async throws
Input
| Parameter | Description |
|---|---|
channelIds *Type: [String]Default: n/a | Array of channel IDs to add to the group. |
Output
This method doesn't return any value.
Sample code
Add channels by their IDs.
1
Remove channels
Remove Channel entities from a channel group with remove(channels:).
Method signature
This method takes the following parameters:
1channelGroup.remove(channels: [ChannelImpl]) async throws
Input
| Parameter | Description |
|---|---|
channels *Type: [ChannelImpl]Default: n/a | Array of Channel entities to remove from the group. |
Output
This method doesn't return any value.
Sample code
Remove channels from a channel group.
1
Remove channel identifiers
Remove channels from a group by ID with removeChannelIdentifiers(_:).
No validation
This method does not validate that the channel IDs exist in the group.
Method signature
This method takes the following parameters:
1channelGroup.removeChannelIdentifiers(_ channelIds: [String]) async throws
Input
| Parameter | Description |
|---|---|
channelIds *Type: [String]Default: n/a | Array of channel IDs to remove from the group. |
Output
This method doesn't return any value.
Sample code
Remove channels by their IDs.
1
Watch channel group
Subscribe to all channels in the group and receive messages with connect(). The callback fires whenever a message arrives on any channel in the group.
Method signature
connect() returns an asynchronous stream that produces a new value whenever someone sends a message on any channel in the group.
This method has the following signature:
1channelGroup.connect() -> AsyncStream<MessageImpl>
Input
This method doesn't take any parameters.
Output
| Type | Description |
|---|---|
AsyncStream<MessageImpl> | An asynchronous stream that emits a new value whenever a new message is published on any channel in the group. |
Sample code
Start receiving messages from all channels in a group.
- AsyncStream
- Closure
1
1
Get present users
Get a list of users currently present on any channel in the group with whoIsPresent().
Requires Presence
Enable Presence for your keyset in the Admin Portal.
Method signature
This method has the following signature:
1channelGroup.whoIsPresent(
2 limit: Int = 1000,
3 offset: Int? = 0
4) async throws -> [String: [String]]
Input
| Parameter | Description |
|---|---|
limitType: IntDefault: 1000 | Maximum number of occupants to return per channel. Valid range: 0-1000. Use 0 to get occupancy counts without user details. |
offsetType: Int?Default: 0 | Zero-based starting index for pagination. Returns occupants starting from this position in the list. Must be >= 0. |
Output
| Type | Description |
|---|---|
[String: [String]] | A dictionary where each key is a channel ID and the value is an array of user IDs present on that channel. |
Sample code
Get all users present on channels in a group.
1
Stream presence
Receive real-time updates when users join or leave any channel in the group with streamPresence().
Requires Presence
Enable Presence for your keyset in the Admin Portal.
Method signature
streamPresence() returns an asynchronous stream that produces a new value whenever presence changes on any channel in the group.
This method has the following signature:
1channelGroup.streamPresence() -> AsyncStream<[String: [String]]>
Input
This method doesn't take any parameters.
Output
| Type | Description |
|---|---|
AsyncStream<[String: [String]]> | An asynchronous stream that emits a dictionary where each key is a channel ID and the value is an array of user IDs present on that channel. |
Sample code
Stream presence updates for all channels in a group.
- AsyncStream
- Closure
1
1