PubNub Subscribe with Channel Groups in JavaScript


Example

With Stream Controller add-on enabled, you can use Channel Groups to subscribe to a 1000's of channels from a single client. You do this by creating a channel group and adding channels to the channel group. We'll assume pubnub variable has been initialized properly with your keys.

Create a generic callback handler function:

function displayCallback(m,e,c,d,f){
    console.log(JSON.stringify(m, null, 4));
}

Create channel group and add channels to it:

pubnub.channel_group_add_channel({
  callback: displayCallback,
  error: displayCallback,
  channel_group: "sports",
  channel: "football,baseball,basketball,lacrosse,cricket"
});

Now, subscribe to the channel group and you will be subscribed to all channels in that group:

pubnub.subscribe({
  callback: displayCallback,
  error: displayCallback,
  channel_group: "sports"
});

Any messages published to the channels in the channel group will be received in the displayCallback function.