androidcouchbasecouchbase-litecouchbase-sync-gateway

Getting error "WebSocket connection closed by peer" when I try Replicator with Couchbase lite 2.7 Android


This class provide the couchbase lite database and the replicator in mode PUSH_AND_PULL to send and receive data from sync_gateway

class CouchBaseLiteManager(private val context: Context) {
    private val NAME_DB = "db"
    private val REMOTE_URI = "ws://localhost:4984/db"
    private val user = "..."
    private val password = "..."
    private lateinit var configuration: DatabaseConfiguration
    private lateinit var mDatabase: Database
    private lateinit var mReplicator: Replicator
    private lateinit var mReplicatorPush: Replicator
    private lateinit var mReplicatorPull: Replicator
    init {
        instantiate()
        createDatabase()
        instantiateReplicator()
        instantiateReplicatorPush()
        instantiateReplicatorPull()
    }

    fun getDatabase(): Database = this.mDatabase!!
    fun getDatabaseName(): String = this.NAME_DB
    fun getReplicatorPush(): Replicator = this.mReplicatorPush

    private fun instantiate() {
        // Initialize the Couchbase Lite system
        CouchbaseLite.init(context)
    }

    private fun createDatabase(){
        // Get the database (and create it if it doesn’t exist).
        configuration = DatabaseConfiguration()
        mDatabase = Database(NAME_DB, configuration)
    }

    private fun instantiateReplicatorPush() {
        var uri:URI ? = null
        try {
            uri = URI(REMOTE_URI)
        }catch (c: CouchbaseLiteException) {
            c.printStackTrace()
        }
        uri?.let {
            val endpoint = URLEndpoint(it)
            val config = ReplicatorConfiguration(mDatabase,endpoint)
            config.replicatorType = CustomReplicatorType.getPushAndPull()
            config.isContinuous = true
            config.authenticator = BasicAuthenticator(user,password)
            //send to remote Endpoint
            config.pushFilter = ReplicationFilter { document, flags ->
                true
            }
            config.setPullFilter { document, flags ->
                true
            }
            mReplicatorPush = Replicator(config)
        }
    }

 }

In this portion of code I start the replicator but I got the error: {Repl#16} Got LiteCore error: WebSocket error 1001 "WebSocket connection closed by peer"

    fun test_couchbase_lite() {
        val manager = CouchBaseLiteManager(context)
        val db = manager.getDatabase()


        Injection.getReplicatorPush().start()
        Injection.getReplicatorPush().addChangeListener(object: ReplicatorChangeListener {
            override fun changed(change: ReplicatorChange) {
                Log.d("debug","replicator push and pull change !!!!! *")
                Log.d("debug","${change.status} -")

                if (change.replicator.status.activityLevel == CustomReplicatorType.getIDLE()) {
                    Log.d("debug", "Scheduler Completed");
                }
                if (change.replicator.status.activityLevel == CustomReplicatorType.getStopped()
                    || change.replicator.status.activityLevel == CustomReplicatorType.getOffline()) {
                    Log.d("debug", "ReplicationTag Stopped");
                }
            }
        })
    }

What is wrong ? or I missed something ! FYI: I use the community edition thanks !


Solution

  • To fix the issue, I have to get the IP address and put it in the xml file network configuration to manage cleartext support.

    According to https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted