Adding documents with a channel and then syncing via channel filtering

I am having a problem getting channel filtering to work in my Xamarin.Android app. As per the documentation on developing channels you can add a channels property to each document:

Adding a channels property to each document is the easiest way to map documents to channels. The channels property is an array of strings that contains the names of the channels to which the document belongs.

which i have done like so:

public void addContent(Database mDatabase,String mMessage,String mChannel)
var docProperties = new Dictionary<string,object> {

        var document = mDatabase.CreateDocument ();
  document.PutProperties (docProperties);		


Now When i read the documentation for replicating channels to couchbase lite it states:

To replicate channels to Couchbase Lite, you configure the replication to use a filter named sync_gateway/bychannel with a filter parameter named channels. The value of the channels parameter is a comma-separated list of channels to fetch. The replication from Sync Gateway now pulls only documents tagged with those channels.

After reading this i modified my replication method to include the filters:

private void startReplication()
var url = new Uri (“”+dbName);

var push = mDatabase.CreatePushReplication (url);
var pull = mDatabase.CreatePullReplication (url);

var channel = new List () { “Channel1” }; // Added for filtering

pull.Filter=“sync_gateway/bychannel”; // Added for filtering
pull.Channels = channel; // Added for filtering

push.Continuous = true;
pull.Continuous = true;


I then uninstall the app from my device and reinstall it so that i can see if the Channel1 documents a replicated but i get the following error in my application output:

Puller: Couchbase.Lite.Replicator.Puller: Received invalid doc ID from _changes: System.Collections.Generic.Dictionary`2[System.String,System.Object]

The replication works fine if i comment out the channel filters but then all documents are replicated and that’s not what im trying to achieve. Can someone tell me what im doing wrong? I would like to achieve programmatic client channel filtering if possible otherwise im open to suggestions.

My sync gateway config file looks like this:

“log”: [“CRUD”, “CRUD+”, “HTTP”, “HTTP+”, “Access”, “Cache”, “Shadow”, “Shadow+”, “Changes”, “Changes+”],
“username”: “bucket”,
“password”: “1234”,
“GUEST”:{“disabled”:false,“admin_channels”: ["*"]}

The whole test project can be found on github:

Heres a screenshot of one of the documents in the data bucket:

The “channels” property in the doc should be an array of strings, as described in the first documentation extract you included above. It looks like your sample doc (seen in the Admin UI console) is setting the channels property as a simple string, which is resulting in the _sync.channels property not getting set.

Hi @adamf

Thanks for pointing out my error, its working now :smile: You would not believe the amount of times I re read the documentation to make sure I was implementing channels the correct way…

Thanks for the feedback - glad it’s working for you now. I’ll flag the channels documentation as a priority for improved documentation.