Adding documents with a channel and then syncing via channel filtering


#1

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> {
{“type”,docType},
{“message”,mMessage},
{“channels”,mChannel},
{“created”,DateTime.Now.ToString()}
};

        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 (“http://192.168.2.28:4984/”+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;

push.Start();
pull.Start();
}

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+”],
“databases”:{
“channeltestdb”:{
“server”:“http://localhost:8091”,
“bucket”:“bucket”,
“username”: “bucket”,
“password”: “1234”,
“users”:{
“GUEST”:{“disabled”:false,“admin_channels”: ["*"]}
}
}
}
}

The whole test project can be found on github: https://github.com/GariceZa/CBChannelsTest

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


#2

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.


#3

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…


#4

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


Channels not being respected on replication