Channel Group Events Design Pattern

Modified on: Tue, Jan 22, 2019 at 1:01 PM

There are definite advantages to using Channel Groups when you need to subscribe to many channels. Channel groups allow you to subscribe to 2000 channels at once by subscribing to a channel group that has those channels added to it. Clients can also subscribe to a maximum of 10 channel groups for a total of 20K channels!

Your clients may not need to listen to that many channels but channel groups make it possible for your server to manage the channels that the client is subscribed to by adding and removing channels on behalf of the clients. This provides additional security among other benefits but there are a few things you might want to implement to add more value to your channel groups implementation.

When your client subscribes to a channel group, it must already contain a channel or the client will receive a 400 Bad Request error response. So before your client subscribes to a channel group, be sure to add a default channel that never gets removed. This will not only prevent the 400 empty group error scenario but the channel can also serve as a channel group events feature. "What kind of events", you ask? Well, anything you desire but for starters, it may be very beneficial, or necessary, for you client app to know when a channel is added or removed, or when the channel group is full (has 2000 channels) or empty (only has the default channel in it - remember, let's avoid getting that 400 response). Now, this doesn't mean that your end user has to know when these actions happen, but your app may need to take some action under the hood.

Since your server is the entity that must do the channel adds and removes (manage permission should never be given to a client app), it will be simple to bundle all this logic in one place to fire these events on this default channel group channel that you added.