How to setup sync URL in couchbase sync gateway to connect to couchbase server

I am trying to use CouchBase Lite for my mobile app. I have seen in tutorial of couchbase but it shows how to add sync URL for walrus server not couchbase server. How can I set up sync URL for couchbase server.

Following is the code which I am using but unable to put or get data from couchbase server

private URL createSyncURL(boolean isEncrypted){ URL syncURL = null; String host = "127.0.0.1"; String port = "8091"; bucketName = "sync_gateway"; try { syncURL = new URL(host + ":" + port + "/" + bucketName); } catch (MalformedURLException me) { me.printStackTrace(); } return syncURL; }

Below is my Sync gateway conf.json

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

and Below is the main activity class of android

package com.couchbase.examples.couchbaseevents;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;

import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Document;
import com.couchbase.lite.Manager;
import com.couchbase.lite.android.AndroidContext;
import com.couchbase.lite.replicator.Replication;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    public static final String DB_NAME = "couchbaseevents";
    final String TAG = "CouchbaseEvents";
    Database database = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "Begin Couchbase Events App");
    Manager manager = null;
    try {
        manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
        database = manager.getDatabase(DB_NAME);
    } catch (Exception e) {
        Log.e(TAG, "Error getting database", e);
        return;
    }
    Document createdDocument = createDocument(database);
    Log.d(TAG, "createdDocument=" + String.valueOf(createdDocument.getProperties()));
    // retrieve the document from the database
    Document retrievedDocument = database.getDocument(createdDocument.getId());
// display the retrieved document
    Log.d(TAG, "retrievedDocument=" + String.valueOf(retrievedDocument.getProperties()));
    updateDoc(database, createdDocument.getId());
    Document updatedDocument = database.getDocument(createdDocument.getId());
    Log.d(TAG, "updatedDocument=" + String.valueOf(updatedDocument.getProperties()));
    try {
        startReplications();
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
    Log.d(TAG, "End Couchbase Events App");
    }
    private void updateDoc(Database database, String documentId) {
    Document document = database.getDocument(documentId);
    try {
        // Update the document with more data
        Map<String, Object> updatedProperties = new HashMap<String, Object>();
        updatedProperties.putAll(document.getProperties());
        updatedProperties.put("eventDescription", "Everyone is invited!");
        updatedProperties.put("address", "123 Elm St.");
        // Save to the Couchbase local Couchbase Lite DB
        document.putProperties(updatedProperties);
    } catch (CouchbaseLiteException e) {
        Log.e(TAG, "Error putting", e);
    }
    }
    private Document createDocument(Database database) {
    // Create a new document and add data
    Document document = database.createDocument();
    String documentId = document.getId();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "Big Party");
    map.put("location", "My House");
    try {
        // Save the properties to the document
        document.putProperties(map);
    } catch (CouchbaseLiteException e) {
        Log.e(TAG, "Error putting", e);
    }
    Document document1 = database.createDocument();
    documentId = document1.getId();
    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("name", "Yagyank");
    map1.put("location", "GGn");
    try {
        // Save the properties to the document
        document1.putProperties(map1);
    } catch (CouchbaseLiteException e) {
        Log.e(TAG, "Error putting", e);
    }
    return document1;
    }
    private URL createSyncURL(boolean isEncrypted){
    URL syncURL = null;
    String host = "https://127.0.0.1";
    String port = "8091";
    String dbName = "sync_gateway";
    try {
        syncURL = new URL(host + ":" + port + "/" + dbName);
    } catch (MalformedURLException me) {
        me.printStackTrace();
    }
    return syncURL;
    }
    private void startReplications() throws CouchbaseLiteException {
    Replication pull = database.createPullReplication(this.createSyncURL(false));
    Replication push = database.createPushReplication(this.createSyncURL(false));
    pull.setContinuous(true);
    push.setContinuous(true);
    pull.start();
    push.start();
      /*  if(!push.isRunning()){
        Log.d(TAG, "MyBad");
    }*/
    /*if(!push.isRunning()) {
        Log.d(TAG, "Replication is not running due to " +push.getLastError().getMessage());
        Log.d(TAG, "Replication is not running due to " +push.getLastError().getCause());
        Log.d(TAG, "Replication is not running due to " +push.getLastError().getStackTrace());
        Log.d(TAG, "Replication is not running due to " +push.getLastError().toString());
    }*/
    }

}

CouchBase Lite connected to Sync Gateway, Sync Gateway connected to CouchBase Server.
Sync Gateway connected walrus server by default.
I think you should set up sync to connect CouchBase Server by this tutorial.

I have setup the sync gateway to connect to couchbase and it is connected to couchbase because when I edit bucket using admin consol in couchbase server I get hit at the sync gateway server. But I am unable to insert data in couchbase server using sync gateway

what do you mean this?
Caution : Do not add, modify or remove data in the bucket using Couchbase APIs or the admin UI, or you will confuse Sync Gateway.

is there some error logs?
can you post your Sync Gateway configuration file ?

I know I should not use admin consol but was only testing whether sync agteway was connected to my bucket or not. I have posted the config file as well as the main activity file of android app

As your configuration file says, you should use 4986 as port and databases as database name to connect sync gateway,
try to change

    String host = "https://127.0.0.1";
    String port = "8091";
    String dbName = "sync_gateway";

to

   String host = "https://127.0.0.1";
    String port = "4986";
    String dbName = "databases";

you can follow this rest api to check the sync gateway.

Still not able to insert data. One more thing, when I go to 127.0.0.1:4986/databases I get

{"error":"not_found","reason":"no such database \"databases\""}

But When I go to 127.0.0.1:4986/sync_gateway I get

{"committed_update_seq":1,"compact_running":false,"db_name":"sync_gateway","disk_format_version":0,"instance_start_time":1462274474240238,"purge_seq":0,"state":"Online","update_seq":1}

So I changed dbName to sync_gateway but still no luck in adding documents

sorry , my mistake the database name is sync_gateway,not `` databases` ```.
you are using http or https?
if you use https, I think you should follow this doc.

I am using http and in my code I have changed host from https://127.0.0.1 to 127.0.0.1

what error when you insert data?
do you follow this rest api?

I am not getting any error in adroid studio. How do I use the rest API. Should I use it with post man?

yes,you can use post man,or curl command to test.

I am getting this output

{ "id": "ButterChicken1 HTTP", "ok": true, "rev": "1-6bcc28edbeaf0d5f9ed87f204820c63f" }

And I can see data in couchbase but body is different

yes,sync gateway will add _sync property for ACL.
you can check the body of docs by get document rest api.

Ok But why cant I insert document using my android app? What is the issue?

have you read this guide?
generally, you should insert document into CouchBase Lite directly, then set up pull/push replication with Sync Gateway, Then document will be insert into Bucket.you also need make a ACL/routing rule by Sync Function.

Ya I read that and in my android app code above I am doing the same first creating the document then retrieveing it and then updating and then doing pull and push request, still not working :frowning:

Can you post your Sync Gateway logs and app logs(adb logcat)?

Do you have any idea where the logs for sync gateway are made?