Error connecting Flutter Application to Sync Gateway/CBS in AWS

  • I am trying to connect to our Couchbase database from a Flutter application.
  • I installed the couchbase database on AWS http://x.x.x.198 (aws-marketplace/couchbase6.5.0-UbuntuLTS18.04)
  • I installed the sync gateway on separate AWS instance http://x.x.x

The sync gateway config file :

{
"logging": {
"console": {
"log_keys": ["*"]
}
},
"CORS": {
"Origin": [
"*"
],
"LoginOrigin": [
"*"
],
"Headers": [
"Content-Type"
],
"MaxAge": 1728000
},
"adminInterface": "127.0.0.1:4985",
"interface": "0.0.0.0:4984",
"databases": {
"XXX": {
"server": "ec2-x-x-x-198.eu-west-2.compute.amazonaws.com:8091/",
"bucket": "XXX",
"username": "XXX",
"password": "*************",
"enable_shared_bucket_access": true,

"import_docs": true,
"users": {
"GUEST": {
"disabled": true

},
"randr_public": {
"password": "********************",
"email": "*****************",
"admin_channels": [
"randr_public"
]
}
},
"use_views": true,
"sync": "function (doc, oldDoc) {if (doc._deleted) { return; } requireAccess(doc.channels); channel(doc.channels);}"
}
}
}

I get the following error in Flutter code :

E/CouchbaseLite/REPLICATOR(17577): {Repl#12}==> N8litecore4repl10ReplicatorE /data/user/0/com.registerandrecall.randrbuild/files/rr_public.cblite2/ ->ws://ec2-18-x-x-198.eu-west-2.compute.amazonaws.com:4984/randr/_blipsync @0x7cca31fac8
E/CouchbaseLite/REPLICATOR(17577): {Repl#12} Got LiteCore error: WebSocket error 1001 "WebSocket connection closed by peer"
I/call.randrbuil(17577): Waiting for a blocking GC ProfileSaver
I/call.randrbuil(17577): WaitForGcToComplete blocked ProfileSaver on HeapTrim for 19.579ms
E/CouchbaseLite/REPLICATOR(17577): {Repl#13}==> N8litecore4repl10ReplicatorE /data/user/0/com.registerandrecall.randrbuild/files/rr_public.cblite2/ ->ws://ec2-18-x-x-198.eu-west-2.compute.amazonaws.com:4984/randr/_blipsync @0x7d447368c8
E/CouchbaseLite/REPLICATOR(17577): {Repl#13} Got LiteCore error: WebSocket error 1001 "WebSocket connection closed by peer"
I/flutter (17577): start notification management for android ...
I/flutter (17577): Replocator -> ReplicatorActivityLevel.connecting
I/flutter (17577): Error in replicator: WebSocket connection closed by peer
I/flutter (17577): Replocator -> ReplicatorActivityLevel.connecting
I/flutter (17577): Error in replicator: WebSocket connection closed by peer
I/flutter (17577): Replocator -> ReplicatorActivityLevel.offline
I/flutter (17577): Replocator -> ReplicatorActivityLevel.connecting
I/flutter (17577): Error in replicator: WebSocket connection closed by peer
I/flutter (17577): Replocator -> ReplicatorActivityLevel.connecting
I/flutter (17577): Error in replicator: WebSocket connection closed by peer
I/flutter (17577): Replocator -> ReplicatorActivityLevel.offline
I/flutter (17577): Replocator -> ReplicatorActivityLevel.connecting
E/CouchbaseLite/REPLICATOR(17577): {Repl#14}==> N8litecore4repl10ReplicatorE /data/user/0/com.registerandrecall.randrbuild/files/rr_public.cblite2/ ->ws://ec2-18-134-140-198.eu-west-2.compute.amazonaws.com:4984/randr/_blipsync @0x7d447363c8
E/CouchbaseLite/REPLICATOR(17577): {Repl#14} Got LiteCore error: WebSocket error 1001 "WebSocket connection closed by peer"
I/flutter (17577): Error in replicator: WebSocket connection closed by peer
I/flutter (17577): Replocator -> ReplicatorActivityLevel.connecting
I/flutter (17577): Error in replicator: WebSocket connection closed by peer
I/flutter (17577): Replocator -> ReplicatorActivityLevel.offline

Sync gateway is running on correct port :

ubuntu@ip-x-x-x-195:/home/sync_gateway$ sudo lsof -i -P -n | grep LISTEN
systemd-r   735 systemd-resolve   13u  IPv4  17323      0t0  TCP 127.0.0.53:53 (LISTEN)
sshd       1093            root    3u  IPv4  20932      0t0  TCP *:22 (LISTEN)
sshd       1093            root    4u  IPv6  20943      0t0  TCP *:22 (LISTEN)
sync_gate 12877    sync_gateway   11u  IPv6 144840      0t0  TCP *:4984 (LISTEN)
sync_gate 12877    sync_gateway   16u  IPv4 144842      0t0  TCP 127.0.0.1:4985 (LISTEN)

and the sg_info.log

2020-11-20T09:58:52.251Z [INF] DCP: c:randr-SGI Restarting vb 1019 using metadata sequence 4  (backfill 0 not in [4-4])
2020-11-20T09:58:52.252Z [INF] DCP: c:randr-SGI Restarting vb 1020 using metadata sequence 6  (backfill 6 not in [6-6])
2020-11-20T09:58:52.252Z [INF] DCP: c:randr-SGI Restarting vb 1021 using metadata sequence 6  (backfill 0 not in [6-6])
2020-11-20T09:58:52.252Z [INF] DCP: c:randr-SGI Restarting vb 1022 using metadata sequence 0  (backfill 0 not in [0-0])
2020-11-20T09:58:52.253Z [INF] DCP: c:randr-SGI Restarting vb 1023 using metadata sequence 5  (backfill 0 not in [5-5])
2020-11-20T09:58:52.253Z [INF] DCP: c:randr-SGI DCP feed starting with name SGI-v-2.8-commit--uuid-fe698846-2b16-11eb-bd8c-02227b0cd494
2020-11-20T09:58:52.253Z [INF] DCP: Starting mutation feed on bucket randr due to either channel cache mode or doc tracking (auto-import)
2020-11-20T09:58:52.253Z [INF] DCP: Using DCP feed for bucket: "randr" (based on feed_type specified in config file)
2020-11-20T09:58:52.287Z [INF] CBGoUtilsLogger: Using plain authentication for user <ud>randr</ud>
2020-11-20T09:58:52.300Z [INF] DCP: c:randr-SG DCP feed starting with name SG-v-2.8-commit--uuid-fe70c24a-2b16-11eb-bd8c-02227b0cd494
2020-11-20T09:58:52.301Z [INF] Cache: Waiting 976.270155ms for sequence allocation...
2020-11-20T09:58:52.340Z [INF] CBGoUtilsLogger: Using plain authentication for user <ud>randr</ud>
2020-11-20T09:58:52.387Z [INF] DCP: Backfill in progress: 0% (1 / 1882)
2020-11-20T09:58:53.283Z [INF] Using metadata purge interval of 3.00 days for tombstone compaction.
2020-11-20T09:58:53.283Z [INF] Created background task: "Compact" with interval 24h0m0s
2020-11-20T09:58:53.353Z [INF] Access: Computed roles for "":
2020-11-20T09:58:53.355Z [INF] Auth: Saved principal w/ name:, seq: #1198
2020-11-20T09:58:53.355Z [INF] Reset guest user to config
2020-11-20T09:58:53.356Z [INF] DCP: Received #1198 ("_user/")
2020-11-20T09:58:53.356Z [INF] Starting admin server on 127.0.0.1:4985
2020-11-20T09:58:53.358Z [INF] Starting server on 0.0.0.0:4984 ...
2020-11-20T09:58:58.360Z [INF] Replicate: Starting sg-replicate replications...

Some troubleshooting tips

  • Can you access the Sync Gateway directly via browser ?

  • If Sync Gateway is behind a load balancer, ensure that websockets is enabled on the load balancer

  • Turn on debug level logging on Sync Gateway and see what. From the log snippet, it doesn’t seem like the sync gateway is seeing a any replication request from the client so something in front of it is potentially blocking the connection

I have few questions that are unrelated to connection issue 0

  • any reason you have “use_views” as true ?
  • And do you have multiple clusters and are you setting inter-sync gateway replications ? I noticed that “sg-replicate” message in logs.
  • curious how you built the Flutter client ? Since we do not natively support Flutter in Couchbase Lite - what are you using?
  • What version of CBS and sync gateway are you using?

We didnt set any thing as load balancer

We will try

No multiple clusters, and we are replicating an existing setup where “use_views” is set as true

Using Flutter couchbase_lite package that is a try to make a clone from Swift couchbase SDK , but is not complete with features such as full text search not supported

CBS : Community Edition 6.5.1 build 6299
Sync Gateway : 2.8.0

hmmm…so you have a single instance of Sync Gateway directly accessible over internet ? What do you see when you try what I suggested below ?

On the unrelated topics-

Do you have plans to move away from views? We would recommend the use of GSI which is the default config. In the future, GSI will likely be the only supported option.
SO thats your entire config file and I see that you have no inter sync gateway replication setup - wondering why that log message is showing up (separate note for me)

So its your own implementation of Flutter plugin. Curious if have you explored the building a dart bindings layer with dart.ffi on top of {Couchbase Lite for C project] (GitHub - couchbase/couchbase-lite-C: C language bindings for the Couchbase Lite embedded NoSQL database engine) and using it within your Flutter app?