Channel Groups API for Ruby 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 Ruby SDK:

Maximum number of channels

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

1channel_registration(
2 action: :add,
3 channels: channels,
4 channel_groups: channel_groups,
5 http_sync: http_sync,
6 callback: callback
7)
* required
ParameterDescription
action *
Type: Symbol
Action to perform; to add, use :add.
channels *
Type: String, Symbol
The channels to add to channel groups.
channel_groups *
Type: String, Symbol
The channel groups to add channels to.
http_sync
Type: Boolean
Default false. Executes asynchronously and returns a future. If true, returns an array of envelopes (or a single envelope).
callback
Type: Lambda accepting one parameter
Callback for each envelope. For async methods a future is returned; call value to retrieve the Envelope.

Sample code

Add channels

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.
1require 'pubnub'
2
3def add_channels_to_group(pubnub)
4 # Async without callback
5 pubnub.channel_registration(action: :add, channel: 'my_channel', channel_group: :somegroup) do |envelope|
6 if envelope.status[:error]
7 puts "Async Error: #{envelope.status[:error]}"
8 else
9 puts "Async Success: Channels added to channel group."
10 end
11 end
12
13 # Sync without callback
14 begin
15 envelopes = pubnub.channel_registration(action: :add, channel: 'my_channel', channel_group: :somegroup, http_sync: true)
show all 35 lines

Response

1#<Pubnub::Envelope:0x007fd385096220
2 @status = {
3 :code => 200,
4 :category => :ack,
5 :error => false,
6 }
7>

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 Ruby SDK:

1channel_registration(
2 action: :get,
3 channel_group: group,
4 http_sync: http_sync,
5 callback: callback
6)
* required
ParameterDescription
action *
Type: Symbol
The action to perform; to get all channels from a channel group, specify :get.
channel_groups *
Type: String, Symbol
The channel groups for which to list channels.
http_sync
Type: Boolean
Default false. Executes asynchronously and returns a future. If true, returns an array of envelopes (or a single envelope).
callback
Type: Lambda accepting one parameter
Callback for each envelope. For async methods a future is returned; call value to retrieve the Envelope.

Sample code

List channels

1pubnub.channel_registration(action: :get, group: 'family') do |envelope|
2 pp envelope
3end

Response

1#<Pubnub::Envelope:0x007fd385856cf8
2 @result = {
3 :data => {
4 "channels" => ["ben"],
5 "group" => "family"
6 }
7 }
8 @status = {
9 :code => 200
10 }
11>

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 Ruby SDK:

1channel_registration(
2 action: :remove,
3 channels: channels,
4 channel_groups: group,
5 http_sync: http_sync,
6 callback: callback
7)
* required
ParameterDescription
action *
Type: Symbol
The action to perform; to remove channels, specify :remove.
channels *
Type: String, Symbol
The channels to remove from channel groups.
channel_groups *
Type: String, Symbol
The channel groups from which to remove channels.
http_sync
Type: Boolean
Default false. Executes asynchronously and returns a future. If true, returns an array of envelopes (or a single envelope).
callback
Type: Lambda accepting one parameter
Callback for each envelope. For async methods a future is returned; call value to retrieve the Envelope.

Sample code

Remove channels

1pubnub.channel_registration(action: :remove, channel: 'son', group: 'family') do |envelope|
2 pp envelope
3end

Response

1#<Pubnub::Envelope:0x007fd385096220
2 @status = {
3 :code => 200,
4 :category => :ack,
5 :error => false,
6 }
7>

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 Ruby SDK:

1channel_registration(
2 action: :remove,
3 channel_groups: channel_groups,
4 http_sync: http_sync,
5 callback: callback
6)
* required
ParameterDescription
action *
Type: Symbol
Use :remove to remove the channel groups.
channel_groups *
Type: String, Symbol
The channel groups to remove.
http_sync
Type: Boolean
Default false. Executes asynchronously and returns a future. If true, returns an array of envelopes (or a single envelope).
callback
Type: Lambda accepting one parameter
Callback for each envelope. For async methods a future is returned; call value to retrieve the Envelope.

Sample code

Delete channel group

1pubnub.channel_registration(action: :remove, channel_group: 'family') do |envelope|
2 pp envelope
3end

Response

1#<Pubnub::Envelope:0x007fd385096220
2 @status = {
3 :code => 200,
4 :category => :ack,
5 :error => false,
6 }
7>
Last updated on