On this page

Channel Groups API for Go SDK

Channel groups allow PubNub developers to bundle thousands of channels into a group that can be identified by a name. These channel groups can then be subscribed to, receiving data from the many back-end channels the channel group contains.

Channel group operations

You can't publish to a channel group. You can only subscribe to it. To publish within the channel group, you need to publish to each channel individually.

Add channels to a channel group

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the PubNub Admin Portal. Read the support page on enabling add-on features on your keys.

This function adds channels to a channel group.

Method(s)

Use the following method in the Go SDK:

Maximum number of channels

You can add up to 200 channels to a channel group per API call.

1pn.AddChannelToChannelGroup().
2 Channels([]string).
3 ChannelGroup(string).
4 QueryParam(queryParam).
5 Execute()
* required
ParameterDescription
Channels *
Type: []string
The channels to add to the channel group.
ChannelGroup *
Type: string
The channel group to add the channels to.
QueryParam
Type: map[string]string
QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Add channels

1package main
2
3import (
4 "fmt"
5 "log"
6
7 pubnub "github.com/pubnub/go/v7"
8)
9
10func main() {
11 // Configuration for PubNub with demo keys
12 config := pubnub.NewConfigWithUserId("myUniqueUserId")
13 config.SubscribeKey = "demo"
14 config.PublishKey = "demo"
15
show all 37 lines

Response

1{
2 "service" : "channel-registry",
3 "status" : 200,
4 "error" : false,
5 "message" : "OK"
6}

List channels in a channel group

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This function lists all channels in a channel group.

Method(s)

Use the following method in the Go SDK:

1pn.ListChannelsInChannelGroup().
2 ChannelGroup(string).
3 QueryParam(queryParam).
4 Execute()
* required
ParameterDescription
ChannelGroup *
Type: string
The channel group for which to list channels.
QueryParam
Type: map[string]string
QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Sample code

List channels

1pn.ListChannelsInChannelGroup().
2 ChannelGroup("cg").
3 Execute()

Response

MethodDescription
Channels
Type: []string
Yes
Group
Type: string
Yes

Remove channels from a channel group

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This function removes channels from a channel group.

Method(s)

Use the following method in the Go SDK:

1pn.RemoveChannelFromChannelGroup().
2 ChannelGroup(string).
3 Channels([]string).
4 QueryParam(queryParam).
5 Execute()
* required
ParameterDescription
ChannelGroup *
Type: string
The channel group from which to remove the channels.
Channels *
Type: []string
The channels to remove from the channel group.
QueryParam
Type: map[string]string
QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Sample code

Remove channels

1pn.RemoveChannelFromChannelGroup().
2 ChannelGroup("cg").
3 Channels([]string{"ch1", "ch2"}).
4 Execute()

Response

1{
2 Error:<nil>
3 Category:Unknown
4 Operation:Remove Channel From Channel Group
5 StatusCode:200
6 TLSEnabled:true
7 UUID:d9713e5a-6bcb-439a-942e-5ba064f2e5dd
8 AuthKey:
9 Origin:ps.pndsn.com
10 OriginalResponse: {
11 "status": 200,
12 "message": "OK",
13 "service": "channel-registry",
14 "error": false
15 }
show all 18 lines

Delete a channel group

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This function deletes a channel group.

Method(s)

Use the following method in the Go SDK:

1pn.DeleteChannelGroup().
2 ChannelGroup(string).
3 QueryParam(queryParam).
4 Execute()
* required
ParameterDescription
ChannelGroup *
Type: string
The channel group to delete.
QueryParam
Type: map[string]string
QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Sample code

Delete channel group

1pn.DeleteChannelGroup().
2 ChannelGroup("remove-cg").
3 Execute()

Response

1{
2 Error:<nil>
3 Category:Unknown
4 Operation:Remove Channel Group
5 StatusCode:200
6 TLSEnabled:true
7 UUID:650089a0-922c-4de6-b422-7a38a964bf45
8 AuthKey:
9 Origin:ps.pndsn.com
10 OriginalResponse: {
11 "status": 200,
12 "message": "OK",
13 "service": "channel-registry",
14 "error": false
15 }
show all 18 lines
Last updated on