Couchbase lite: Got LiteCore error: WebSocket error 404 Object Not Found

#1

Hi,

After integrating Couchbase lite framework with iOS app, and running the app in Simulator, getting the following error in Xcode console:

2019-05-13 10:23:01.038849-0500 EventDrivenPattern[59750:846411] CouchbaseLite Replicator ERROR: {Repl#2} Got LiteCore error: WebSocket error 404 "Object Not Found"

Error code :: 10404

Error code :: 10404

I have seen some existing answers and it is talking about adding proper database name

databasename

in the URL.

Following is the code, and i think i added the URL in correct manner.

import UIKit

import CouchbaseLiteSwift

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.
    setupCouchbase ()
}


func setupCouchbase () {
    
    // Get the database (and create it if it doesn’t exist).
    let database: Database
    do {
        database = try Database(name: "eventdriven")
    } catch {
        fatalError("Error opening database")
    }
    
    // Create a new document (i.e. a record) in the database.
    let mutableDoc = MutableDocument()
        .setFloat(2.0, forKey: "version")
        .setString("SDK", forKey: "type")
    
    // Save it to the database.
    do {
        try database.saveDocument(mutableDoc)
    } catch {
        fatalError("Error saving document")
    }
    
    // Update a document.
    if let mutableDoc = database.document(withID: mutableDoc.id)?.toMutable() {
        mutableDoc.setString("Swift", forKey: "language")
        do {
            try database.saveDocument(mutableDoc)
            
            let document = database.document(withID: mutableDoc.id)!
            // Log the document ID (generated by the database)
            // and properties
            print("Document ID :: \(document.id)")
            print("Learning \(document.string(forKey: "language")!)")
        } catch {
            fatalError("Error updating document")
        }
    }
    
    // Create a query to fetch documents of type SDK.
    let query = QueryBuilder
        .select(SelectResult.all())
        .from(DataSource.database(database))
        .where(Expression.property("type").equalTo(Expression.string("SDK")))
    
    // Run the query.
    do {
        let result = try query.execute()
        print("Number of rows :: \(result.allResults().count)")
    } catch {
        fatalError("Error running the query")
    }
    
    // Create replicators to push and pull changes to and from the cloud.
    let targetEndpoint = URLEndpoint(url: URL(string: "ws://127.0.0.1:8091/eventdriven")!)
    let replConfig = ReplicatorConfiguration(database: database, target: targetEndpoint)
    replConfig.replicatorType = .pushAndPull
    
    // Add authentication.
    replConfig.authenticator = BasicAuthenticator(username: "xxxxxx", password: "yyyyy")
    
    // Create replicator.
    let replicator = Replicator(config: replConfig)
    
    // Listen to replicator change events.
    replicator.addChangeListener { (change) in
    if let error = change.status.error as NSError? {
        print("Error code :: \(error.code)")
    }
    }
    
    // Start replication.
    replicator.start()
    
}

}

Below is the screenshot of the Couchbase server console:

#2

The URL you gave is ws://127.0.0.1:8091/eventdriven — that’s the wrong port number. 8091 is for direct connections to Couchbase Server. You want 4984, which is the (default) port Sync Gateway listens on.

#3

I tried both,

let targetEndpoint = URLEndpoint(url: URL(string: “wss://127.0.0.1:4984/eventdriven”)!)

let targetEndpoint = URLEndpoint(url: URL(string: “ws://127.0.0.1:4984/eventdriven”)!)

but i am getting “POSIX error 61” error:

2019-05-13 11:50:38.100372-0500 EventDrivenPattern[63220:925989] CouchbaseLite Replicator ERROR: {Repl#50} Got LiteCore error: POSIX error 61 "Connection refused"

#4

Ok, I fixed POSIX error 61. That was due to not running Sync gateway. I run sync gateway now and also iOS app. It says the following error in Xcode console:
2019-05-13 14:12:59.481387-0500 EventDrivenPattern[67725:1024421] CouchbaseLite Replicator ERROR: {Repl#2} Got LiteCore error: WebSocket error 404 "Not Found"

Looks like the error is “Database not found”. Because, sync gateway shows "2019-05-13T14:12:59.475-05:00 [INF] HTTP: #001: --> 404 no such database “eventdriven” (0.5 ms)
"
I would like to know what is the database name here? Is it already available in console ? Is it name of cluster? Can i give new name here?

#5

I checked “basic-couchbase-bucket.json” file and it shows dbname as “db”

{
“logging”: {
“console”: {
“log_keys”: [""]
}
},
“databases”: {
“db”: {
“server”: “http://localhost:8091”,
“bucket”: “default”,
“users”: { “GUEST”: { “disabled”: false, “admin_channels”: ["
"] } },
“allow_conflicts”: false,
“revs_limit”: 20
}
}
}

I tried, > let targetEndpoint = URLEndpoint(url: URL(string: “ws://127.0.0.1:4984/db”)!)

in iOS code, but still shows Xcode console error as

2019-05-13 15:50:57.655255-0500 EventDrivenPattern[71847:1112244] CouchbaseLite Replicator ERROR: {Repl#2} Got LiteCore error: WebSocket error 404 "Not Found"

Sync gateway:

2019-05-13T15:50:57.649-05:00 [INF] HTTP: #013: --> 404 no such database “db” (0.1 ms)

#6

How did you start sync gateway? Did you pass this JSON file as an argument?

#7

i started sync gateway just in termial ‎ going to ⁨▸ ⁨couchbase-sync-gateway⁩ ▸ ⁨bin⁩ and “open sync_gateway”

#8

You have to pass the config file as an argument. I’m sure this is in the documentation.

#9

Could you pls point me the documentation? I didn’t get it.

I followed this link: https://docs.couchbase.com/sync-gateway/current/getting-started.html
it says simply,
Option 1: Start Sync Gateway from the Command Line

  1. Unpack the tar.gz installer.
sudo tar -zxvf couchbase-sync-gateway-community_2.1.3_x86_64.tar.gz
  1. Launch the sync_gateway program from the command line.
~/Downloads/couchbase-sync-gateway/bin/sync_gateway
#10

On the same page you sent there is an entire section dedicated to starting Sync Gateway…

#11

True. I got that. I followed to create

sync-gateway-config.json

{
“log”: [""],
“databases”: {
“db”: {
“server”: “http://localhost:8091”,
“bucket”: “eventdriven”,
“username”: “Administrator”,
“password”: “xyz”,
“enable_shared_bucket_access”: true,
“import_docs”: “continuous”,
“users”: {
“GUEST”: { “disabled”: false, “admin_channels”: ["
"] }
},
“sync”: function (doc, oldDoc) { if (doc.sdk) { channel(doc.sdk); } }
}
}
}

I tried to run like below and got some other error.

O2771-C02K648W:~ user$ /Users/xxx/couchbase-sync-gateway/bin/sync_gateway /Users/xxx/couchbase-sync-gateway/examples/sync-gateway-config.json

2019-05-14T00:51:33.261-05:00 [WRN] Using deprecated config option: log. Use logging.console.log_keys instead. – rest.(*ServerConfig).deprecatedConfigLoggingFallback() at config.go:623

2019-05-14T00:51:33.261-05:00 [ERR] No logFilePath configured, and --defaultLogFilePath flag is not set. Log files required for product support are not being generated. – base.(*LoggingConfig).Init() at logging_config.go:40

2019-05-14T00:51:33.261-05:00 ==== Couchbase Sync Gateway/2.1.3(4;7143b13) ====

2019-05-14T00:51:33.261-05:00 [INF] Console LogKeys: [* HTTP]

2019-05-14T00:51:33.261-05:00 [INF] Console LogLevel: info

2019-05-14T00:51:33.261-05:00 [INF] Log Redaction Level: none

2019-05-14T00:51:33.261-05:00 [INF] Configured process to allow 5000 open file descriptors

2019-05-14T00:51:33.261-05:00 [INF] Opening db /db as bucket “eventdriven”, pool “default”, server http://localhost:8091

2019-05-14T00:51:33.261-05:00 [INF] GoCBCustomSGTranscoder Opening Couchbase database eventdriven on http://localhost:8091 as user “Administrator”

2019-05-14T00:51:33.269-05:00 [INF] Auth: Attempting credential authentication http://localhost:8091?http_idle_conn_timeout=90000&http_max_idle_conns=64000&http_max_idle_conns_per_host=256

2019-05-14T00:51:33.273-05:00 [INF] Successfully opened bucket

2019-05-14T00:51:33.276-05:00 [INF] Initializing indexes with numReplicas: 1…

2019-05-14T00:51:33.286-05:00 [INF] Query: Index sg_syncDocs_x1 doesn’t exist, creating…

2019-05-14T00:51:33.289-05:00 [ERR] Error opening database db: Unable to install index syncDocs: Error installing Couchbase index: sg_syncDocs_x1: Unable to create indexes with the specified number of replicas (1). Increase the number of index nodes, or modify ‘num_index_replicas’ in your Sync Gateway database config. – rest.RunServer() at config.go:903

What does it mean? Why is this error coming now?

#12

This means you don’t have enough nodes in your cluster to satisfy the index replica requirements of your Sync Gateway setup (num_index_replicas in your database setting). The default value is 1 which means you need one extra node to serve as a backup for the index service. Change it to 0 if you only have one node.

#13

Oh. The documentation doesn’t talk about

num_index_replicas

https://docs.couchbase.com/sync-gateway/current/getting-started.html

After adding num_index_replicas=0, looks like it is connecting now. Please correct and give us easy documentation.

But still Xcode shows like below, not sure why.

2019-05-14 09:11:00.922566-0500 EventDrivenPattern[81252:1233767] CouchbaseLite Replicator ERROR: {Repl#2} Got LiteCore error: WebSocket error 401 "Unauthorized"

Also, it is adding some dunmy values in CB console like below screenshot for reference.

Thanks for the help.

#14

I fixed the issue by changing port to : 4985 instead of 4984
For ex: in iOS app,
let targetEndpoint = URLEndpoint(url: URL(string: “ws://127.0.0.1:4985/db”)!)

#15

@jongladwin, @jens, @borrrden,

#16

The “unauthorized” error is because guest access is disabled. Make sure your config file still has “GUEST”: { “disabled”: false … it seems as though that’s gone missing.

Using port 4985 is OK for bringup, but not in real use. That’s the admin port, which allows anyone access to anything, kind of like superuser access to Sync Gateway. It’s only available from localhost.

#17

Ok got it. But, “GUEST”: { “disabled”: false - is already there, that didn’t not fix though. Only after adding port 4985, got it fixed.

#18

Your guest has no admin_channels (your post has an empty array) so basically it will have access to nothing…

Seems you made a copy paste error.

#19

This is what i have: “GUEST”: { “disabled”: false, “admin_channels”: [“public”] }

#20

What is the output of http://localhost:4985/_config ?