On this page

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(id: string): ChannelGroup

Input

* required
ParameterDescription
id *
Type: string
Default:
n/a
Unique identifier for the channel group.

Output

TypeDescription
ChannelGroup
A ChannelGroup object you can use to manage the group.

Sample code

Get a reference to a channel group named my-channel-group.

1const channelGroup = chat.getChannelGroup("my-channel-group")

Remove channel group

Delete a channel group from the server with removeChannelGroup().

Method signature

This method takes the following parameters:

1chat.removeChannelGroup(id: string): Promise<void>

Input

* required
ParameterDescription
id *
Type: string
Default:
n/a
Unique identifier of the channel group to remove.

Output

TypeDescription
Promise<void>
Returns an empty response when the channel group is successfully removed.

Sample code

Remove a channel group named my-channel-group.

1await chat.removeChannelGroup("my-channel-group")

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,
3 sort?: object,
4 limit?: number,
5 page?: {
6 next?: string,
7 prev?: string
8 }
9}): Promise<{
10 channels: Channel[],
11 page: {
12 next: string,
13 prev: string
14 },
15 total: number
show all 16 lines

Input

* required
ParameterDescription
filter
Type: string
Default:
n/a
Expression used to filter the results. Returns only the channels whose properties satisfy the given expression. The filter language is defined here.
sort
Type: object
Default:
n/a
An object to specify the sort order. Available options are id, name, and updated. Use asc or desc to specify the sorting direction. For example: { name: "asc" }.
limit
Type: number
Default:
100
Number of objects to return in response. The default (and maximum) value is 100.
page
Type: object
Default:
n/a
Object used for pagination to define which previous or next result page you want to fetch.

Output

TypeDescription
Promise<{ channels, page, total }>
Promise containing a set of channels with pagination information (next, prev, total).

Sample code

List all channels in a channel group.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3const { channels, page, total } = await channelGroup.listChannels()
4
5channels.forEach(channel => {
6 console.log("Channel:", channel.id)
7})

Add channels

Add Channel entities to a channel group with addChannels().

Method signature

This method takes the following parameters:

1channelGroup.addChannels(channels: Channel[]): Promise<void>

Input

* required
ParameterDescription
channels *
Type: Channel[]
Default:
n/a
Array of Channel entities to add to the group.

Output

TypeDescription
Promise<void>
Returns an empty response when channels are successfully added.

Sample code

Add two channels to a channel group.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3const supportChannel = await chat.getChannel("support-channel")
4const generalChannel = await chat.getChannel("general-channel")
5
6await channelGroup.addChannels([supportChannel, generalChannel])

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(ids: string[]): Promise<void>

Input

* required
ParameterDescription
ids *
Type: string[]
Default:
n/a
Array of channel IDs to add to the group.

Output

TypeDescription
Promise<void>
Returns an empty response when channel identifiers are successfully added.

Sample code

Add channels by their IDs.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3await channelGroup.addChannelIdentifiers(["support-channel", "general-channel"])

Remove channels

Remove Channel entities from a channel group with removeChannels().

Method signature

This method takes the following parameters:

1channelGroup.removeChannels(channels: Channel[]): Promise<void>

Input

* required
ParameterDescription
channels *
Type: Channel[]
Default:
n/a
Array of Channel entities to remove from the group.

Output

TypeDescription
Promise<void>
Returns an empty response when channels are successfully removed.

Sample code

Remove channels from a channel group.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3// Assuming you have channel references
4await channelGroup.removeChannels([supportChannel, generalChannel])

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(ids: string[]): Promise<void>

Input

* required
ParameterDescription
ids *
Type: string[]
Default:
n/a
Array of channel IDs to remove from the group.

Output

TypeDescription
Promise<void>
Returns an empty response when channel identifiers are successfully removed.

Sample code

Remove channels by their IDs.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3await channelGroup.removeChannelIdentifiers(["support-channel", "general-channel"])

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

This method takes the following parameters:

1channelGroup.connect(callback: (message: Message) => void): () => void

Input

* required
ParameterDescription
callback *
Type: (message: Message) => void
Default:
n/a
Callback function invoked whenever a message is received on any channel in the group.

Output

TypeDescription
() => void
Function you can call to stop listening for new messages.

Sample code

Start receiving messages from all channels in a group.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3const disconnect = channelGroup.connect((message) => {
4 console.log(`Received message on channel ${message.channelId}: ${message.text}`)
5})
6
7// Later, when you want to stop receiving messages
8disconnect()

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 params?: {
3 limit?: number,
4 offset?: number
5 }
6): Promise<{ [channelId: string]: string[] }>

Input

* required
ParameterDescription
params
Type: object
Default:
n/a
Object containing optional pagination parameters.
 → limit
Type: number
Default:
1000
Maximum number of occupants to return per channel. Valid range: 0-1000. Use 0 to get occupancy counts without user details.
 → offset
Type: number
Default:
n/a
Zero-based starting index for pagination. Returns occupants starting from this position in the list. Must be >= 0.

Output

TypeDescription
Promise<{ [channelId: string]: string[] }>
A map 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.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3const presenceByChannel = await channelGroup.whoIsPresent()
4
5Object.entries(presenceByChannel).forEach(([channelId, userIds]) => {
6 console.log(`Channel ${channelId} has users:`, userIds)
7})

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

This method takes the following parameters:

1channelGroup.streamPresence(
2 callback: (presenceByChannels: { [channelId: string]: string[] }) => void
3): () => void

Input

* required
ParameterDescription
callback *
Type: ({ [channelId: string]: string[] }) => void
Default:
n/a
Callback function invoked when presence changes. Receives a map where each key is a channel ID and the value is an array of user IDs present on that channel.

Output

TypeDescription
() => void
Function you can call to stop receiving presence updates.

Sample code

Stream presence updates for all channels in a group.

1const channelGroup = chat.getChannelGroup("my-channel-group")
2
3const stopPresenceStream = channelGroup.streamPresence((presenceByChannel) => {
4 Object.entries(presenceByChannel).forEach(([channelId, userIds]) => {
5 console.log(`Channel ${channelId} now has users:`, userIds)
6 })
7})
8
9// Later, when you want to stop receiving presence updates
10stopPresenceStream()
Last updated on