Sync Gateway Not Syncing Custom Collection to Mobile Database

I am new to Couchbase and need guidance to resolve the following issue:

When I start a replication process from my React Native Android app (using Kotlin), the default collection syncs correctly if I do not configure a custom scope and collection in the Sync Gateway database. However, when using a custom collection, the data does not sync. I receive an error log in the Sync Gateway indicating a problem.

2025-02-05 11:00:26 2025-02-05T05:30:26.057Z [INF] HTTP: c:#008 db:sampledb GET /sampledb/_blipsync (as <ud>syncuser</ud>)
2025-02-05 11:00:26 2025-02-05T05:30:26.057Z [INF] HTTP+: c:[53456386] db:sampledb #008:     --> 101 [53456386] Upgraded to WebSocket protocol BLIP_3+CBMobile_3 (as <ud>syncuser</ud>)  (0.0 ms)
2025-02-05 11:00:26 2025-02-05T05:30:26.057Z [INF] WS: c:#008 db:sampledb Start BLIP/Websocket handler
2025-02-05 11:00:26 2025-02-05T05:30:26.066Z [INF] SyncMsg: c:[53456386] db:sampledb #1: Type:getCheckpoint   --> 400 Collection property not specified and default collection is not configured for this database Time:55.009µs
2025-02-05 11:00:26 2025-02-05T05:30:26.068Z [INF] SyncMsg: c:[53456386] db:sampledb #2: Type:proposeChanges   --> 400 Collection property not specified and default collection is not configured for this database Time:12.612µs
2025-02-05 11:00:26 2025-02-05T05:30:26.073Z [INF] SyncMsg: c:[53456386] db:sampledb #3: Type:subChanges   --> 400 Collection property not specified and default collection is not configured for this database Time:20.531µs
2025-02-05 11:00:26 2025-02-05T05:30:26.082Z [INF] WS: c:#008 db:sampledb Error: receiveLoop exiting with WebSocket error: failed to get reader: received close frame: status = StatusCode(4002) and reason = "Unrecognized collection"
2025-02-05 11:00:26 2025-02-05T05:30:26.133Z [INF] WS: c:#008 db:sampledb BLIP/Websocket Handler exited with error: failed to get reader: received close frame: status = StatusCode(4002) and reason = "Unrecognized collection"
2025-02-05 11:00:26 2025-02-05T05:30:26.133Z [INF] HTTP: c:[53456386] db:sampledb #008:     --> BLIP+WebSocket connection error: failed to get reader: received close frame: status = StatusCode(4002) and reason = "Unrecognized collection"
2025-02-05 11:00:26 2025-02-05T05:30:26.133Z [INF] HTTP: c:[53456386] db:sampledb #008:    --> BLIP+WebSocket connection closed

Configuration in syn gateway
1.) DB config

{
    "bucket": "sample",
    "scopes": {
        "scope1": {
            "collections": {
                "collection1": {
                    "sync": "function(doc){channel(\"*\");}",
                    "import_filter": "function(doc) { return true; }"
                }
            }
        }
    },
    "name": "sampledb",
    "import_docs": true,
    "enable_shared_bucket_access": true,
    "num_index_replicas": 0
}

2.) User config

{
    "name": "syncuser",
    "admin_channels": [
        "!",
        "*",
        "project1"
    ],
    "collection_access": {
        "scope1": {
            "collection1": {
                "admin_channels": [
                    "!",
                    "*",
                    "project1"
                ],
                "all_channels": [
                    "!",
                    "*",
                    "project1"
                ]
            }
        }
    },
    "email": "",
    "disabled": false
}

React native andriod code(Kotlin)

var database = Database("sampledb")
val config = ReplicatorConfigurationFactory.newConfig(
        collections = mapOf(database.collections to null),  
        target = URLEndpoint(URI("ws://IPADDRESS/sampledb")),
        type = ReplicatorType.PUSH_AND_PULL,
        authenticator = BasicAuthenticator("syncuser", "helloworld".toCharArray())
    )

    val replicator = Replicator(config)
    replicator.start()

How can I resolve this issue?

Hi Arul,

The set of collections on the CBL client for replication need to match, or be a subset of the collections on Sync Gateway.

From the looks of the logs, the CBL client is still trying to replicate the _default scope and _default collection, and since they’re no longer defined on the Sync Gateway config replication doesn’t work.

I’m not an expert on the react native implementation, but I suspect database.collections contains named collections as well as the default scope/collection.

You’ll want to be limiting your replicator config to only a subset of those present on Sync Gateway instead.

E.g:

 const config = new ReplicatorConfiguration(target);
 config.addCollection(collectionName);
1 Like

Thank you! Your response saved me a lot of time.

From the reference link you provided, I learned that we can specify a custom collection in the replication configuration, and it will sync accordingly.

For my project using com.couchbase.lite:couchbase-lite-android-ktx:3.2.1, I added a CollectionConfiguration as the second parameter in the addCollection method.

Here’s the updated code snippet I used to start replication:

val target = URLEndpoint(URI("ws://IPAddress/sampledb"));
val auth = BasicAuthenticator("user", "Password".toCharArray());
val config = ReplicatorConfiguration(target);
config.addCollection(collection,CollectionConfiguration());
config.setAuthenticator(auth);
config.setContinuous(true);

val replicator = Replicator(config);
replicator.start()
1 Like