1

I'm having a problem pulling data via Sync Gateway channels.

The way I understand channels is they are basically a form of tag that will allows you to mark a document in a special way.

What I am trying to do

When I close the application, delete the local db, and then reopen the application, I am expecting all of the documents in the channels that were set to be pulled, but instead nothing is pulled.

Setup

I am using Couchbase Lite 1.4.0 and the latest Sync_Gateway.

Sync Gateways config file, I am using the default sync function:

{
    "databases": {
        "db": {
            "server": "http://127.0.0.1:8091",
            "username": "db",
            "password": "pass",
            "users":{
                "user1":{
                    "password":"pass"
                }
            }
        }
    }
}

I am accessing sync gateway in Couchbase lite like so:

private String[] docChannels = new String[]{
    "channel1",
    "channel2",
};
private String[] configChannels = new String[]{
    "config1",
    "config2",
};

URL url = null;
try {
    url = new URL("http://127.0.0.1:4984/db");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

Replication push = d.createPushReplication(url);
Replication pull = d.createPullReplication(url);
Replication pullConfig = d.createPullReplication(url);

pull.setChannels(Arrays.asList(docChannels));
pullConfig.setChannels(Arrays.asList(configChannels));

pullConfig.setContinuous(false);
pull.setContinuous(true);
push.setContinuous(true);

Authenticator auth = AuthenticatorFactory.createBasicAuthenticator("user1", "pass");
push.setAuthenticator(auth);
pull.setAuthenticator(auth);
pullConfig.setAuthenticator(auth);

push.start();

pullConfig.start();
pull.start();

Whenever I create a document, I add the channels key with a value of ["config1"].

My document's sync info now looks like:

"_sync": {
    "rev": "1-87cdc8c1fd5e0e4ce1a0897cbd47aca1",
    "sequence": 4,
    "recent_sequences": [
      4
    ],
    "history": {
      "revs": [
        "1-87cdc8c1fd5e0e4ce1a0897cbd47aca1"
      ],
      "parents": [
        -1
      ],
      "channels": [
        [
          "config1"
        ]
      ]
    },
    "channels": {
      "config1": null
    },
    "time_saved": "2017-09-22T13:20:43.6061974-05:00"
  }

I am not sure what I am doing wrong here. Pushing to the Couchbase server works fine, but my pulling does not.

Thanks.

Hypnic Jerk
  • 1,192
  • 3
  • 14
  • 32

1 Answers1

1

In order for the document to be synced to another device the logged in user needs to have the document's channel added to the user's channel list. In this case by adding "admin_channels": ["config1"]

So the sync gateway config would look like this...

{
    "databases": {
        "db": {
            "server": "http://127.0.0.1:8091",
            "username": "db",
            "password": "pass",
            "users":{
                "user1":{
                    "password":"pass",
                    "admin_channels": ["config1"]
                }
            }
        }
    }
}
combinatorial
  • 9,132
  • 4
  • 40
  • 58
  • As far as I'm aware if the sync function isn't specified, it defaults to the default one, which is what you've suggested. – Hypnic Jerk Sep 23 '17 at 00:05
  • I wasn't aware of that. You also need to add the channels that you require to sync to the user. e.g. "admin_channels": ["config1"] for user1 – combinatorial Sep 23 '17 at 14:13
  • Well, that worked. It seems that I was misunderstanding an important aspect, I'll have to read more in depth on it. Edit your answer and I'll accept it – Hypnic Jerk Sep 23 '17 at 14:17
  • OK, will do, did you confirm that the default sync function is as above? Or did you change that too? – combinatorial Sep 23 '17 at 16:12
  • 2
    It is as above. Based on the docs, https://developer.couchbase.com/documentation/mobile/current/guides/sync-gateway/sync-function-api-guide/index.html#default-sync-function it applies it if one is not supplied. – Hypnic Jerk Sep 23 '17 at 17:25
  • 2
    That is one way to do it, but you can also use the `access()` function inside the sync function to dynamically give users access. If it is meant to be permanent, then using admin_channels is the easiest way though. – borrrden Sep 24 '17 at 00:58