Get all users who have access to a channel within the sync function

I would like to get all the user ID’s that have access to a channel from the sync gateway. For example, if I grant two different users access to a channel. Then a new document comes in that would be assigned to that channel, I would like to get the array of users that have access to that channel first.

I need the array of users who have access to that channel so that I can send out an APNS (apples push notification) to the users of that channel.

Ideally, it would be nice if I could just do
var usr_arr = channel(the_channel).users()

Off the top of my head, I don’t believe there is a way to fetch the list of users associated with a channel .
Given a user, you can fetch the channel associated with it.
So one option would be to get the list of all users using GET /_user all an then iterate over each user to get details of the user (which would include the channel info for that user) .
Probably not the most efficient if you are dealing with 1000s of users…

Another option, assuming each user has a user document, would be to include some information within each document itself , similar to a role (that would then be associated with a channel) . You can use that information to query for matching user documents.

There may be some better options…@Traun?

@priya.rajagopal is right. There is no API for this. Another way to handle it is by listening to the changes feed. You can see an outline of doing this in the comments on this post:

Thank you for the feedback. I was putting off creating a backend server just for apple push notifications. Is there a way to send APN’s directly from the sync function? If not, I think I will just use Node.js and monitor the change feed for both new “message” documents and “userchannel” documents. Essentially, maintaining my own hash of channel to user access.

I’m setting up a location base chatting feature where messages are geo-tagged and added to a channel based on location. Eg. channel(msg_ch_lat_1234_lon_1234). Then each user has their own “userchannel” document which list the channels they have access too.

Life would just be too easy if I could do the following from the sync function.


There isn’t a way to direct send APNS from sync gateway. You have the option of posting to an external URL based on Document Change events. So another option instead of monitoring _changes feed is for you to trigger an event to your node.js or equivalent backend service everytime the “user channel” document changes for instance.
Then your backend app can send out the push notification. If you don’t want to build out APNS end point yourself, you may want to consider using a third party service like Urban Airship - won’t be free but you don’t have to implement the APNS code your self.

Curious - this implies to me that you have no backend application service - so is all your business logic is in your mobile apps ?

Just to add to this - it sounds similar to an idea I was playing around with a while ago. As it happens, I decided to go down a different route, but I raised Allow formatting/customisation of DocumentChangeEvent webhook data · Issue #3148 · couchbase/sync_gateway · GitHub to track the idea. Feel free to weight in on that issue if you think it’s relevant or have any ideas around this.

Just one more bit of info. An API for this has been discussed. There’s a GitHub issue open on it:

Currently, yes. All the business logic has been on the mobile app. The app is fairly simple, so up until now we have been able to simply filter the replication and do any database logic on the app. However, we will soon be porting some of that logic to the back in node.js we are currently setting up. Which leads to another question. I am having trouble connecting swagger on my node.js to the sync gateway. However, I will pose the question on a new thread. Thanks