How to work around channels in Couchbase Lite?

Hi,
Im doing application like single applicaiton for multiple clients, for this i want to create multiple Buckets. but i got some solution from by using channels. Please assist me, how to use or create channels in couchbase Lite iOS.

I read channels but there is no code for Couchbase Lite iOS.

Hi, channels can be created inside the sync function of the sync gateway. When your Couchbase Lite iOS DB syncs with an authenticated user, it will sync only with the channels you gave it access to in the sync function.

hi, thanks for reply, can you please assist me, how to do this, like for example when i create new user from couchbase lite, can i add any channel key to new user document with array of channel names.

1 Like

@itssrinadh

The Sync Gateway uses a default sync function, which is equivalent to:

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

This will tag every document with the channel names that are defined in the documents “channels” property e.g. the following document would be tagged with three channels called “chan1”,“chan2” and “chan3”

{
    "channels":["chan1","chan2","chan3"] 
}

For a user to receive the document via sync they must have access to one or more of the channels the document is mapped to.

Channel access can be granted to a user via the ADMIN API when creating or updating the user by passing the “admin_channels” property e.g. To give a user access to the “chan1” channel the admin_channels property would be:

"admin_channels":["chan1"]

Users can also be given access to a channel dynamically via the sync function using the ‘access’ method e.g we could change the sync_function to:

function (doc) {
   channel(doc.channels);
   access(doc.owner, doc.channels);
}

You would change your document to:

{
    "owner":"user1"
    "channels":["chan4","chan5"] 
}

The document would be tagged with the channels “chan4” and “chan5” and ‘user1’ would be given access to channels “chan4” and “chan5”.

When a CBL client authenticates with Sync Gateway as ‘user1’ and initiates a pull replication, the document will be sync’d to the client.

There is an introduction to channels and the sync function here

For a more complete example of a sync function and using channels to partition data between users, take a look at one of the ToDoLite demo projects e.g. Android

1 Like

@andy Thanks for reply, Let me know have you any sample for ios.

ok can i do like this,

when i create a document with CBL, i will add channels key with array of channels string, so this doc will be available for particular channels.

Channels work with sync gateway and are controlled by your sync gateway config file. Do you have sync gateway setup on a server and your iOS app is connecting to it? If you have your iOS app connecting to sync gateway and it is using the default config then you should be able to just add a channels property to a document as you mentioned in your last post. But if you want more control over how channels are created for documents then you will need to learn how to modify your sync gateway config function. The main documentation for that seems to be here: http://developer.couchbase.com/documentation/mobile/1.1.0/develop/guides/sync-gateway/configuring-sync-gateway/config-properties/index.html

They also have a doc on how to use channels: http://developer.couchbase.com/documentation/mobile/1.1.0/develop/guides/sync-gateway/channels/index.html

You can use the access function in your sync gateway config function to grant users access to channels (there is an example above in a previous post), but be very careful with that function. It has terrible performance and if you put it in a frequently updated document type it could bring down your server.

Another option for granting user’s access to channels is through the Sync Gateway Admin API, also mentioned in a post above. Just remember that you set admin_channels and admin_roles together. You cannot change one without changing the other. E.g. Doing a PUT to the Admin Rest API for a user with body = {“admin_roles”:“role1”} would clear any roles and admin_channels previously assigned to that user. Best practice is to retrieve any existing admin_roles and admin_channels first, and then either add to them or change them to what you want and then write both back in your PUT request.

@alexegli Thank you, i will check it.

this is a very valuable thread for understanding channels. I think this should be promoted to the documentation. Specifically the examples provided with the context of Couchbase Lite.

@theprojectabot, I had work on Channels. thanks for your response on this query.