Not able to connect couch mobile db to sync_gateway

I am very new to couchbase , I have set up couchbase server , sync_gate and a android app using coucbase mobile lite.

I am trying to sync the local remote on android app to sync_gateway but I am not able to sync it.

my json file is as

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

and this is what what my sync_gate looks like …

C:\Program Files (x86)\Couchbase>sync_gateway.exe config.json

2016-01-18T12:45:38.493-05:00 Enabling logging: [CRUD+ REST+ Changes+ Attach+]
2016-01-18T12:45:38.496-05:00 ==== Couchbase Sync Gateway/1.1.1(10;2fff9eb) ====
2016-01-18T12:45:38.498-05:00 Configured Go to use all 4 CPUs; setenv GOMAXPROCS to override this
2016-01-18T12:45:38.501-05:00 Opening db /sync_gateway as bucket “sync_gateway”, pool “default”, server http://localhost:8091
2016-01-18T12:45:38.506-05:00 Opening Couchbase database sync_gateway on http://localhost:8091
2016/01/18 12:45:38 Trying with http://127.0.0.1:8091/pools/default/bucketsStreaming/sync_gateway
2016/01/18 12:45:38 Trying with selected node 0
2016/01/18 12:45:38 Got new configuration for bucket sync_gateway
2016/01/18 12:45:39 Trying with selected node 0
2016-01-18T12:45:39.518-05:00 NOTE: “sync_gateway”'s sync function has changed. The new function may assign different channels to documents, or permissions to users. You may want to re-sync the database to update these.
2016/01/18 12:45:41 go-couchbase: call to ViewCustom(“sync_gateway”, “access”) in github.com/couchbase/sync_gateway/db.(*DatabaseContext).ComputeChannelsForPrincipal took 1.6873498s
2016-01-18T12:45:41.338-05:00 Reset guest user to config
2016-01-18T12:45:41.363-05:00 Starting admin server on 127.0.0.1:4985
2016-01-18T12:45:41.371-05:00 Starting server on :4984 …
2016-01-18T12:45:42.681-05:00 Changes+: Notifying that “sync_gateway” changed (keys="{_sync:user:}") count=2

I am trying to push and pull the data from sync_gateway . Android code looks like -

protected void onCreate(Bundle savedInstanceState) {

    Log.d("starting ", " |----------->");

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    try{
      String DatabaseName = "couchbaseevents";
        Manager manager = new Manager( new AndroidContext(getApplicationContext()),Manager.DEFAULT_OPTIONS);

        Database database = manager.getDatabase(DatabaseName);
      android.util.Log.d("Message", "Database created");

        Document document = database.getDocument("1379");
        Log.i("Message", "Document with ID : " + document.getId());

        HashMap<String,Object> mymap = new HashMap<String, Object>();

        mymap.put("Name", "pandey");
        mymap.put("Address", "India");
        document.putProperties(mymap);

        Document doc1 = database.getExistingDocument("1377");

        Map<String, Object > result = doc1.getProperties();

        Log.i("Name", result.get("Name").toString());

        Log.i("Adress", result.get("Address").toString());
    

       String dns = "http://localhost";
        String port = "4984";
        String database1 = "sync_gateway";

        URL syncUrl = new URL("http://localhost:4984/sync_gateway/");

        Log.i("----", "URL created");
        Replication pull= database.createPullReplication(syncUrl);
        Log.i("----", "Repliation");
        pull.start();
        Log.i("----", "Push start");

        Document doc = database.getExistingDocument("ping");

        Map<String, Object > result = doc.getProperties();

        Log.i("Name", result.get("name").toString());

        Log.i("Adress", result.get("address").toString());


    }
    catch (Exception e)
    {
        android.util.Log.e("CouchbaseEvents","error nconnectiong to DB"+e.getMessage()+", "+e.getStackTrace());
    }

I am getting below error -

s I/art: Not late-enabling -Xcheck:jni (already on)
01-18 13:35:08.963 19582-19582/com.example.sujee.couchbaseevents D/starting: |----------->
01-18 13:35:09.464 19582-19588/com.example.sujee.couchbaseevents W/art: Suspending all threads took: 39.050ms
01-18 13:35:09.522 19582-19592/com.example.sujee.couchbaseevents I/art: Background sticky concurrent mark sweep GC freed 23355(1365KB) AllocSpace objects, 5(160KB) LOS objects, 49% free, 1866KB/3MB, paused 5.249ms total 110.747ms
01-18 13:35:09.544 19582-19582/com.example.sujee.couchbaseevents V/TDCollateJSON: SQLite3 handle is -1274527616
01-18 13:35:09.547 19582-19582/com.example.sujee.couchbaseevents I/----: URL created
01-18 13:35:09.557 19582-19582/com.example.sujee.couchbaseevents I/----: Repliation
01-18 13:35:09.559 19582-19582/com.example.sujee.couchbaseevents I/----: Pull start
01-18 13:35:09.597 19582-19616/com.example.sujee.couchbaseevents E/RemoteRequest: io exception. url: http://localhost:4984/sync_gateway/_local/8751eaea00a525de709b3c356d4fd78ac3cb8619
org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:4984 refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:193)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
at com.couchbase.lite.support.RemoteRequest.executeRequest(RemoteRequest.java:201)
at com.couchbase.lite.support.RemoteRequest.run(RemoteRequest.java:104)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 4984) after 60000ms: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:234)
at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
at java.net.Socket.connect(Socket.java:884)

I am not sure what I am doing wrong here . Can anyone please help on this.

The server URL in your Android app is probably wrong. Its host is localhost, which would mean the Android device is trying to connect to itself. Instead you want to use the hostname or the IP address of the computer running Sync Gateway.

Keep in mind that the Android emulator also sets up its own localhost, so it will not be the same as your computer. To get the actual localhost (127.0.0.1) from the emulator, use the address 10.0.2.2.

@borrrden @jens Thanks both you . That was fixed now.

However I am having another issue, When I am pulling the document from sync_gateway , I am getting below error

Attempt to invoke virtual method ‘java.util.Map com.couchbase.lite.Document.getProperties()’ on a null object reference, [Ljava.lang.StackTraceElement;@ee4c98a
01-18 17:12:51.758 10341-10371/com.example.sujee.couchbaseevents D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-18 17:12:51.828 10341-10371/com.example.sujee.couchbaseevents I/OpenGLRenderer: Initialized EGL, version 1.4
01-18 17:12:51.893 10341-10371/com.example.sujee.couchbaseevents W/EGL_emulation: eglSurfaceAttrib not implemented
01-18 17:12:51.893 10341-10371/com.example.sujee.couchbaseevents W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabfbff40, error=EGL_SUCCESS
01-18 17:12:51.899 10341-10351/com.example.sujee.couchbaseevents W/art: Suspending all threads took: 18.710ms
01-18 17:12:51.903 10341-10372/com.example.sujee.couchbaseevents W/Sync: com.couchbase.lite.replicator.PullerInternal@bc6348: starting ChangeTracker with since=null mode=OneShot
01-18 17:12:51.904 10341-10372/com.example.sujee.couchbaseevents W/Sync: com.couchbase.lite.replicator.PullerInternal@bc6348: started ChangeTracker com.couchbase.lite.replicator.ChangeTracker@82665e1
01-18 17:12:52.072 10341-10370/com.example.sujee.couchbaseevents W/Sync: com.couchbase.lite.replicator.PullerInternal@bc6348: Received invalid doc ID from _changes: {seq=1, id=_user/GUEST, changes=[]}
01-18 17:12:52.073 10341-10374/com.example.sujee.couchbaseevents W/ChangeTracker: com.couchbase.lite.replicator.ChangeTracker@82665e1: Change tracker calling stop (OneShot)
01-18 17:12:52.074 10341-10374/com.example.sujee.couchbaseevents W/ChangeTracker: com.couchbase.lite.replicator.ChangeTracker@82665e1: Change tracker calling changeTrackerStopped, client: com.couchbase.lite.replicator.PullerInternal@bc6348
01-18 17:12:52.097 10341-10387/com.example.sujee.couchbaseevents W/ChangeTracker: com.couchbase.lite.replicator.ChangeTracker@82665e1: Change tracker not calling changeTrackerStopped, client == null
01-18 17:12:52.099 10341-10387/com.example.sujee.couchbaseevents E/Sync: PullerInternal stopGraceful.run() finished

Also, on sync_gateway I am getting below message

2016-01-18T18:09:16.480-05:00 Enabling logging: [CRUD+ REST+ Changes+ Attach+]
2016-01-18T18:09:16.483-05:00 ==== Couchbase Sync Gateway/1.1.1(10;2fff9eb) ====
2016-01-18T18:09:16.487-05:00 Configured Go to use all 4 CPUs; setenv GOMAXPROCS to override this
2016-01-18T18:09:16.499-05:00 Opening db /sync_gateway as bucket “sync_gateway”, pool “default”, server http://localhost:8091
2016-01-18T18:09:16.510-05:00 Opening Couchbase database sync_gateway on http://localhost:8091
2016/01/18 18:09:16 Trying with http://127.0.0.1:8091/pools/default/bucketsStreaming/sync_gateway
2016/01/18 18:09:16 Trying with selected node 0
2016/01/18 18:09:16 Got new configuration for bucket sync_gateway
2016/01/18 18:09:16 Trying with selected node 0
2016-01-18T18:09:16.754-05:00 Reset guest user to config
2016-01-18T18:09:16.757-05:00 Starting admin server on 127.0.0.1:4985
2016-01-18T18:09:16.777-05:00 Starting server on :4984 …
2016-01-18T18:17:17.157-05:00 HTTP: #001: GET /sync_gateway/
2016-01-18T18:17:17.953-05:00 HTTP: #002: GET /favicon.ico/
2016-01-18T18:17:17.956-05:00 HTTP: #002: --> 400 invalid database name “favicon.ico” (56.0 ms)
2016-01-18T18:19:30.440-05:00 HTTP: #003: GET /sync_gateway/_local/32e2d1b34de8d08fe0cdaff4e35f1b539551e6f8
2016-01-18T18:19:30.509-05:00 HTTP: #003: --> 404 missing (70.1 ms)
2016-01-18T18:19:30.647-05:00 HTTP: #004: POST /sync_gateway/_changes
2016-01-18T18:19:30.683-05:00 Changes: MultiChangesFeed({}, {Since:0 Limit:0 Conflicts:true IncludeDocs:false Wait:false Continuous:false Terminator:0xc0820d4420 HeartbeatMs:300000 TimeoutMs:300000}) …
2016-01-18T18:19:30.748-05:00 Changes+: MultiChangesFeed: channels expand to channels.TimedSet{"!":0x1, "
":0x1} …
2016/01/18 18:19:35 go-couchbase: call to ViewCustom(“sync_gateway”, “channels”) in github.com/couchbase/sync_gateway/db.(DatabaseContext).getChangesInChannelFromView took 4.2150184s
2016-01-18T18:19:35.173-05:00 Changes+: MultiChangesFeed sending &{Seq:1 ID:_user/GUEST Deleted:false Removed:{} Doc:map[] Changes:[] Err: branched:false}
2016-01-18T18:19:35.179-05:00 Changes: MultiChangesFeed done
2016-01-18T18:21:21.486-05:00 HTTP: #005: GET /sync_gateway/_local/32e2d1b34de8d08fe0cdaff4e35f1b539551e6f8
2016-01-18T18:21:21.493-05:00 HTTP: #005: --> 404 missing (8.0 ms)
2016-01-18T18:21:21.599-05:00 HTTP: #006: POST /sync_gateway/_changes
2016-01-18T18:21:21.603-05:00 Changes: MultiChangesFeed({
}, {Since:0 Limit:0 Conflicts:true IncludeDocs:false Wait:false Continuous:false Terminator:0xc0820390e0 HeartbeatMs:300000 TimeoutMs:300000}) …

all other code remains same. Can you help me here too …

I am trying to sync the local remote on android app to sync_gateway

The puller will do the opposite of what you want, it will sync the sync gateway contents to your local database. In fact, that’s exactly what it is doing but since it is empty nothing will be pulled. If you want to upload to sync gateway use a pusher instead.

Sorry , I was not clear in my previous question. Let me explain it once again.

I have changed the URL to 10.0.2.2 and I able to connect to sync_gatway ( I am not getting any error in connection now).

However, I was trying to push mobile app data to couchbase server via sync_gateway. But I am getting below error -

Attempt to invoke virtual method ‘java.util.Map com.couchbase.lite.Document.getProperties()’ on a null object reference.

Also, I was trying to pull sync_gateway bucket data from couchbase server via sync_gateway to my local mobile app database but again I am getting same error.

Also I have noticed few common error message on sync_gateway command prompt as below -

2016-01-18T18:09:16.480-05:00 Enabling logging: [CRUD+ REST+ Changes+ Attach+]
2016-01-18T18:09:16.483-05:00 ==== Couchbase Sync Gateway/1.1.1(10;2fff9eb) ====
2016-01-18T18:09:16.487-05:00 Configured Go to use all 4 CPUs; setenv GOMAXPROCS to override this
2016-01-18T18:09:16.499-05:00 Opening db /sync_gateway as bucket “sync_gateway”, pool “default”, server http://localhost:8091
2016-01-18T18:09:16.510-05:00 Opening Couchbase database sync_gateway on http://localhost:8091
2016/01/18 18:09:16 Trying with http://127.0.0.1:8091/pools/default/bucketsStreaming/sync_gateway
2016/01/18 18:09:16 Trying with selected node 0
2016/01/18 18:09:16 Got new configuration for bucket sync_gateway
2016/01/18 18:09:16 Trying with selected node 0
2016-01-18T18:09:16.754-05:00 Reset guest user to config
2016-01-18T18:09:16.757-05:00 Starting admin server on 127.0.0.1:4985
2016-01-18T18:09:16.777-05:00 Starting server on :4984 …
2016-01-18T18:17:17.157-05:00 HTTP: #001: GET /sync_gateway/
2016-01-18T18:17:17.953-05:00 HTTP: #002: GET /favicon.ico/
2016-01-18T18:17:17.956-05:00 HTTP: #002: --> 400 invalid database name “favicon.ico” (56.0 ms)
2016-01-18T18:19:30.440-05:00 HTTP: #003: GET /sync_gateway/_local/32e2d1b34de8d08fe0cdaff4e35f1b539551e6f8
2016-01-18T18:19:30.509-05:00 HTTP: #003: --> 404 missing (70.1 ms)
2016-01-18T18:19:30.647-05:00 HTTP: #004: POST /sync_gateway/_changes
2016-01-18T18:19:30.683-05:00 Changes: MultiChangesFeed({}, {Since:0 Limit:0 Conflicts:true IncludeDocs:false Wait:false Continuous:false Terminator:0xc0820d4420 HeartbeatMs:300000 TimeoutMs:300000}) …
2016-01-18T18:19:30.748-05:00 Changes+: MultiChangesFeed: channels expand to
channels.TimedSet{"!":0x1, "
":0x1} …
2016/01/18 18:19:35 go-couchbase: call to ViewCustom(“sync_gateway”, “channels”) in github.com/couchbase/sync_gateway/db.(DatabaseContext).getChangesInChannelFromView took 4.2150184s
2016-01-18T18:19:35.173-05:00 Changes+: MultiChangesFeed sending &{Seq:1 ID:_user/GUEST Deleted:false Removed:{} Doc:map[] Changes:[] Err: branched:false}
2016-01-18T18:19:35.179-05:00 Changes: MultiChangesFeed done
2016-01-18T18:21:21.486-05:00 HTTP: #005: GET /sync_gateway/_local/32e2d1b34de8d08fe0cdaff4e35f1b539551e6f8
2016-01-18T18:21:21.493-05:00 HTTP: #005: --> 404 missing (8.0 ms)
2016-01-18T18:21:21.599-05:00 HTTP: #006: POST /sync_gateway/_changes
2016-01-18T18:21:21.603-05:00 Changes: MultiChangesFeed({
}, {Since:0 Limit:0 Conflicts:true IncludeDocs:false Wait:false Continuous:false Terminator:0xc0820390e0 HeartbeatMs:300000 TimeoutMs:300000}) …

I am not sure it says invalid database name here ??

Most likely you’re trying to use a Document reference from the database that’s unexpectedly null. Debug this the way you would any other NullPointerException. I’m not an Android developer so I don’t know how it works with your IDE, but you should be able to set a breakpoint that’s hit when an exception is thrown.

@borrrden @jens Thanks alot guys , it working now .

Why I am getting this error?

03-29 07:17:32.071 16377-16434/com.couchbase.grocerysync D/Sync: com.couchbase.lite.support.RemoteRequestRetry$1@53497318: RemoteRequestRetry failed, non-transient error. NOT retrying. url: http://10.200.10.125:8091/grocery-sync/_revs_diff
03-29 07:17:32.071 16377-16434/com.couchbase.grocerysync V/Sync: com.couchbase.lite.replicator.PusherInternal$3@53494254: got /_revs_diff response
03-29 07:17:32.071 16377-16434/com.couchbase.grocerysync E/Sync: com.couchbase.lite.replicator.PusherInternal@5348adc4: Progress: set error = org.apache.http.client.HttpResponseException: Object Not Found
03-29 07:17:32.071 16377-16434/com.couchbase.grocerysync D/Sync: [fireTrigger()] => STOP_GRACEFUL
03-29 07:17:32.071 16377-16413/com.couchbase.grocerysync D/Sync: firing trigger: STOP_GRACEFUL
03-29 07:17:32.071 16377-16413/com.couchbase.grocerysync V/Sync: [onExit()] RUNNING => STOPPING
03-29 07:17:32.071 16377-16413/com.couchbase.grocerysync V/Sync: [onEntry()] RUNNING => STOPPING
03-29 07:17:32.071 16377-16413/com.couchbase.grocerysync D/Sync: com.couchbase.lite.replicator.PusherInternal@5348adc4 STOPPING…

@MoududHassan

Please don’t post questions to old threads, you are more likely to get an answer if you create a new thread.

From your log it looks like you are trying to sync directly with Couchbase Server (port 8091)?, that is not going to work.

The default Public REST API port for Sync Gateway is 4984 and the ADMIN REST API is 4985.

Thanks for your answer, I will never post my question to another question. I configured sync gateway…

NybSyss-Mac-mini:bin nybsys$ sync_gateway /Users/nybsys/Downloads/couchbase-sync-gateway/bin/data/config.json
2016-03-29T13:16:57.871+06:00 Enabling logging: [CRUD+ REST+ Changes+ Attach+]
2016-03-29T13:16:57.871+06:00 ==== Couchbase Sync Gateway/() ====
2016-03-29T13:16:57.871+06:00 Configured process to allow 5000 open file descriptors
2016-03-29T13:16:57.871+06:00 Opening db /grocery-sync as bucket “grocery-sync”, pool “default”, server http://10.200.10.125:8091
2016-03-29T13:16:57.872+06:00 Opening Couchbase database grocery-sync on <10.200.10.125:8091>
2016/03/29 13:16:58 Trying with selected node 0
2016/03/29 13:16:58 Trying with 10.200.10.125:8091/pools/default/bucketsStreaming/grocery-sync
2016/03/29 13:16:58 Got new configuration for bucket grocery-sync
2016/03/29 13:16:58 Trying with selected node 0
2016-03-29T13:16:58.133+06:00 Reset guest user to config
2016-03-29T13:16:58.133+06:00 Starting admin server on 127.0.0.1:4985
2016-03-29T13:16:58.138+06:00 Changes+: Notifying that “grocery-sync” changed (keys="{_sync:user:}") count=2
2016-03-29T13:16:58.139+06:00 Starting server on :4984 …

and my config.json file is

{
“log”:[“CRUD+”, “REST+”, “Changes+”, “Attach+”],
“databases”: {
“grocery-sync”: {
“server”:“http://10.200.10.125:8091”,
“bucket”:“grocery-sync”,
“sync”:function (doc) { channel (doc.channels); },
“users”: {
“GUEST”: {
“disabled”: false,
“admin_channels”: ["*"]
}
}
}
}
}

after config my couchbase server bucket looks like.

I used this url in android app “127.0.0.1:4984/grocery-sync”

and the error is

Sync error: 127.0.0.1:4984 refused

@MoududHassan

Your Sync Gateway configuration looks good now.

But in your android App you pointing your sync URL to localhost 127.0.0.1.

I’m not familiar with Android development, but if you are using a simulator on the same machine as you are running Sync Gateway I think you should use one of the following IP addresses:

Use 10.0.2.2 for default AVD and 10.0.3.2 for Genymotion

I am getting this error now

url: http://10.0.2.2:4984/grocery-sync/_local/7d966c7236cbe2604f87293f8f6b78187f7f2ab1
org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.2.2:4984 timed out

Thanks a lot. Working now :relaxed: