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.

pn.AddChannelToChannelGroup().
Channels([]string).
ChannelGroup(string).
QueryParam(queryParam).
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

package main

import (
"fmt"
"log"

pubnub "github.com/pubnub/go/v7"
)

func main() {
// Configuration for PubNub with demo keys
config := pubnub.NewConfigWithUserId("myUniqueUserId")
config.SubscribeKey = "demo"
config.PublishKey = "demo"

show all 37 lines

Response

{
"service" : "channel-registry",
"status" : 200,
"error" : false,
"message" : "OK"
}

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:

pn.ListChannelsInChannelGroup().
ChannelGroup(string).
QueryParam(queryParam).
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

pn.ListChannelsInChannelGroup().
ChannelGroup("cg").
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:

pn.RemoveChannelFromChannelGroup().
ChannelGroup(string).
Channels([]string).
QueryParam(queryParam).
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

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

Response

{
Error:<nil>
Category:Unknown
Operation:Remove Channel From Channel Group
StatusCode:200
TLSEnabled:true
UUID:d9713e5a-6bcb-439a-942e-5ba064f2e5dd
AuthKey:
Origin:ps.pndsn.com
OriginalResponse: {
"status": 200,
"message": "OK",
"service": "channel-registry",
"error": false
}
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:

pn.DeleteChannelGroup().
ChannelGroup(string).
QueryParam(queryParam).
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

pn.DeleteChannelGroup().
ChannelGroup("remove-cg").
Execute()

Response

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