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.

channel_registration(
action: :add,
channels: channels,
channel_groups: channel_groups,
http_sync: http_sync,
callback: callback
)
* 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.
require 'pubnub'

def add_channels_to_group(pubnub)
# Async without callback
pubnub.channel_registration(action: :add, channel: 'my_channel', channel_group: :somegroup) do |envelope|
if envelope.status[:error]
puts "Async Error: #{envelope.status[:error]}"
else
puts "Async Success: Channels added to channel group."
end
end

# Sync without callback
begin
envelopes = pubnub.channel_registration(action: :add, channel: 'my_channel', channel_group: :somegroup, http_sync: true)
show all 35 lines

Response

#<Pubnub::Envelope:0x007fd385096220
@status = {
:code => 200,
:category => :ack,
:error => false,
}
>

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:

channel_registration(
action: :get,
channel_group: group,
http_sync: http_sync,
callback: callback
)
* 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

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

Response

#<Pubnub::Envelope:0x007fd385856cf8
@result = {
:data => {
"channels" => ["ben"],
"group" => "family"
}
}
@status = {
:code => 200
}
>

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:

channel_registration(
action: :remove,
channels: channels,
channel_groups: group,
http_sync: http_sync,
callback: callback
)
* 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

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

Response

#<Pubnub::Envelope:0x007fd385096220
@status = {
:code => 200,
:category => :ack,
:error => false,
}
>

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:

channel_registration(
action: :remove,
channel_groups: channel_groups,
http_sync: http_sync,
callback: callback
)
* 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

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

Response

#<Pubnub::Envelope:0x007fd385096220
@status = {
:code => 200,
:category => :ack,
:error => false,
}
>
Last updated on