How to use the channels attribute and dynamically subscribing to channels

Hi All,

I saw in the documentation that if my document has an attribute called “channels”, Ex:
{
“channels”: [“myChannel1”, “myChannel2”]
}

Then sync gateway will understand that this is a channel without any channel config. So in theory, I could also dynamically subscribe users via mobile:

ReplicatorConfiguration config = new ReplicatorConfiguration(database, new URLEndpoint(url));
    config.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH_AND_PULL);
    config.setContinuous(true);
    config.setChannels(Arrays.asList("myChannel1", "myChannel2", "myChannel3")
    config.setAuthenticator(new BasicAuthenticator(username, password));

Is it a valid approach? Is there any limit on the number of channels? Because I might have up to 100k channels using this approach.

Yes. This is because the default sync function is simply

function (doc, oldDoc) {
   channel(doc.channels);
}

For reference this default is listed in the documentation

EDIT Although 100k channels is probably an excessive amount. They don’t come for free but I will refer to @bbrks for more detail.

There are no hard-limits to channels, but there are a couple of things you need to be aware of for larger numbers of channels.

Per-document channel limitations are related to the amount of metadata we have available to use in Couchbase Server for documents. With enable_shared_bucket_access=true, this is around 1MB of data. When enable_shared_bucket_access=false, this is 20MB minus the size of your document in bytes.

This is described in the docs here: https://docs.couchbase.com/sync-gateway/current/data-routing.html#channel-count

If you have a large number of channels that are split between a lot of documents, then you can avoid hitting the above limitation, but still hit memory-usage limits on Sync Gateway, as we maintain a cache per channel. These can be tuned using these config parameters depending on how much RAM you have available for Sync Gateway: https://docs.couchbase.com/sync-gateway/current/config-properties.html#databases-foo_db-cache