0

I've got android and web apps. Android app uses Couchbase Lite, web app uses Couchbase. I'm using Couchbase Sync Gateway to enable data replication between those two databases.

So far it works ok for sending data from mobile and receiving it both in web app and second mobile device. I noticed that all send documents have "_sync" parameter added.

My question is how can I enable documents added through web app (to couchbase database) to take part in replication? (they don't have field "_sync" by default)

edit

As Legendary_Hunter suggested I tried using Shadow, but still can't get it working. My config file:

{
"log":["CRUD+", "REST+", "Changes+", "Attach+"],
"databases": {
    "kris_mobile_db": {
        "server":"http://192.168.0.11:8091",
        "sync":`
            function (doc) {
            channel (doc.channels);
        }`,
        "bucket":"kris_mobile_db",
        "users": {
            "GUEST": {
                "disabled": false,
                "admin_channels": ["*"]
            }
        },
        "shadow": {
            "server": "http://localhost:8091",
            "bucket": "kris_mobile_db_sync"
        }
    }
}
}

edit2 (29.05.16)

public class DatabaseManager {

private static DatabaseManager manager;
private static CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().autoreleaseAfter(6000).build();
private static String bucketName = "kris_mobile_db";
private Cluster cluster;
private Bucket bucket;

    public static DatabaseManager getInstance(){

        if(manager == null)
            manager = new DatabaseManager();

        return manager;
    }

    public Bucket getBucketInstance(){
        if(bucket == null)
            bucket = cluster.openBucket(bucketName);

        return bucket;
    }


    public boolean establishConnection(String host, String port, String bucketName){

        // host: 192.168.0.11, port: 8091
        cluster = CouchbaseCluster.create(env, host+":"+port);

        DatabaseManager.bucketName = bucketName;
        bucket = cluster.openBucket(bucketName);

        return true;
    }
}

and inserting is like

JsonDocument doc = JsonDocument.create(docId, content);
DatabaseManager.getInstance().getBucketInstance().insert(doc);

edit3

So finally I managed to get shadowing working. If anyone had the same problem. My basic database is kris_mobile_db and syncGateway shadowing database is kris_mobile_db_sync. Config file:

{
"log":["CRUD+", "REST+", "Changes+", "Attach+"],
"databases": {
    "kris_mobile_db": {
        "server":"http://192.168.0.11:8091",
        "sync":`
            function (doc) {
            channel (doc.channels);
        }`,
        "bucket":"kris_mobile_db_sync",
        "users": {
            "GUEST": {
                "disabled": false,
                "admin_channels": ["*"]
            }
        },
        "shadow":{
            "server":"http://192.168.0.11:8091",
            "bucket":"kris_mobile_db"
        }
    }
}
}
Mohru
  • 725
  • 1
  • 7
  • 17

2 Answers2

0

Just use bucket shadowing. It is bidirectional syncing of sync gateway bucket with any bucket of couchbase server.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Legendary_Hunter
  • 1,040
  • 2
  • 10
  • 29
  • Your solution seems to be the one I'm looking for. But I can't get it working. When I follow the instructions during starting of sync gateway I get warnings: `Error aplying change from external bucket: 409 Not imported -- db.(*Shadower).readTapFeed() at shadower.go:79` and `Error applying change from external bucket: 400 user defined top level properties beggining with '_' ane not allowed in document body -- db.(*Shadower).readTapFeed() at shadower.go:79` and Shadow seems not to be working. Everything works as previously. – Mohru May 14 '16 at 21:07
  • Can I have a look at your sync gateway config file? – Legendary_Hunter May 16 '16 at 15:02
  • Bucket shadowing is not supported by Couchbase anymore and as such is not the recommended way of doing this. – Laurent Doguin May 16 '16 at 21:56
  • Ok, I realized that I've put the 'shadow' part in wrong place, I moved it up and now SyncGateway starts normally but after inserting doc through web I get warning: `WARNING: changeCache: Error unmarchalling doc"Contractor": json cannot unmarchall number into Go value of type db.DocumentRoot ` and then still cannot pull it to mobile. I attached config file to question. – Mohru May 18 '16 at 21:47
  • Are you directly inserting through Web in sync gateway bucket or you inserting through Web in "kris_mobile_db"? – Legendary_Hunter May 22 '16 at 07:01
  • With shadowing I tried both. I was creating Cluster object firstly from "192.168.0.11:8091" (kris_mobile_db adres) and then "192.168.0.11:4984" (SyncGateway adres). Result was the same- documents were inserted (with mentioned above warning- does couchbase have problems with non-string values?), but couldn't be synchronized with mobile. Now, when I try to use SyncGateway REST API given "192.168.0.11:4984" it inserts nothing at all. – Mohru May 22 '16 at 18:00
  • Sounds like you're writing into the wrong bucket. Never modify SG's own bucket. Only modify the other bucket, the one SG is shadowing, and it will detect and pull in the changes. – Jens Alfke May 23 '16 at 18:49
  • @JensAlfke , so let's sum up. I've edited my question and would appreciate if you take a look at it. Having config file as above should such code work? And how should it work? With this code new documents are inserted into the basic bucket but without _sync field (should it be present?) and are not pulled by mobile devices. – Mohru May 29 '16 at 13:28
  • @Mohru Your config file seems to be incorrect. You specify in your file that your sync gateway bucket name is `"kris_mobile_db"` and bucket from which you are shadowing is also `"kris_mobile_db"` . – Legendary_Hunter May 30 '16 at 14:30
  • @Legendary_Hunter I changed my config file (in the question). But still files are saved in the database but not pulled to mobile. I got totally confused about this config file. Is it OK now? Should the `shadow` part be inside specific database configuration section or not? – Mohru Jun 01 '16 at 15:43
  • I think you have interchanged the position of sync bucket and main bucket. shadow should contain name of bucket in which you are inserting manually not sync gateway bucket – Legendary_Hunter Jun 01 '16 at 18:54
  • @Legendary_Hunter now I'm more confused. I was trying to write my config file as here: [link](http://developer.couchbase.com/documentation/mobile/current/develop/guides/sync-gateway/configuring-sync-gateway/config-properties/index.html). I've got `kris_mobile_db` in three places: as database name, as bucket name, as shadow bucket (?) name. Apparently in some places this name is wrong, but I really don't get where should I change it. I tried changing database name but it also isn't working. Could you give me any example? – Mohru Jun 02 '16 at 20:21
  • ok change shadow bucket name to something else. This is the bucket in which you change data manually or using admin panel or using some api. – Legendary_Hunter Jun 03 '16 at 12:55
  • It's finally working! I misunderstood almost everything about the config file... I posted final version in the question. Thank you @Legendary_Hunter ! – Mohru Jun 04 '16 at 12:26
0

If you want to keep all the good things that the Sync Function gives you, than you have to go through the sync gateway. The sync gateway exposes a REST API that you can use to build your web app.

Laurent Doguin
  • 483
  • 2
  • 4
  • What if I already have a bucket which is used in production and has over 1 million documents? Then should I use REST API of sync gateway to move each document? – Legendary_Hunter May 17 '16 at 09:16
  • Yes, You need a way to have your data model compatible with the sync gateway. Reimporting it would definitely work. – Laurent Doguin May 17 '16 at 16:50
  • Isnt that a very tedious and long process, I think bucket shadowing was made only to faster this process and cater this particular problem(Having a bucket already in production). I am also using bucket shadowing and now I am worried I should use it or not – Legendary_Hunter May 17 '16 at 18:13
  • Basically bucket shadowing was discontinued because it does not really work. You should probably not use it. And yes it's a tedious process. Right now the roadmap for couchbase mobile is to integrate sync gateway better and fix that issue. – Laurent Doguin May 17 '16 at 20:03
  • I am not very familiar with SyncGateway REST API. @LaurentDoguin could you give me any hint where to start from? Does it mean that I have to add section _sync to all my documents? – Mohru May 18 '16 at 21:49
  • You need to post your document to the sync directly, without metadata. The _fields will be added by the sync gateway: http://developer.couchbase.com/documentation/mobile/current/develop/references/sync-gateway/rest-api/document-public/put-db-doc/index.html – Laurent Doguin May 19 '16 at 13:58
  • I'm not sure if this is a right place to ask such question, but I tried to follow your hint and [this method](http://stackoverflow.com/questions/34200298/androidhow-to-use-couchbase-rest-api-in-android-application/34204010#34204010) and I'm really not sure I'm giving the right path. I use "http://[localhost]:[sync-gateway port]/[bucket name]" but get 301 error and in sync-gateway console I get `WARNING: changeCache: Error unmarchalling doc"Contractor": json: cannot unmarchall number into Go value of type db.DocumentRoot` – Mohru May 21 '16 at 14:58
  • 1
    (Couchbase mobile architect here.) Bucket shadowing _is_ supported, contrary to what was said above. Sorry for the miscommunication. We do recommend using the SG REST API instead if you're starting from scratch; but if you already have a Couchbase bucket with data you want to replicate, bucket shadowing is the way to go. – Jens Alfke May 23 '16 at 18:46