How to sync data from couchbase server to android app while running android project?

I have ‘todo’ android project and web application. While running andriod project, I insert a todo document into couchbase bucket via php web application. how can i sync todo document into android project (while running android in emulator)?

this is my sync config code
{
“log”: [""],
“adminInterface”: “127.0.0.1:4985”,
“interface”: “0.0.0.0:4984”,
“databases”: {
“todo”: {
“server”: “http://admin:test1234@127.0.0.1:8091”,
“bucket”: “todo_test_sync”,
“users”: {
“admin”:{“password”:“test1234”, “admin_channels”: ["
"]},
“user1”: {“password”: “pass”, “admin_channels”: [“user1”]},
“user2”: {“password”: “pass”, “admin_channels”: [“user2”]}
},
“sync”: `function(doc, oldDoc)
{
if(doc.type==‘task-list’)
{
channel(doc._id);
access(doc.owner,doc._id);
requireUser(doc.owner);

			if(!doc.name || !doc.owner || !doc.type)
			{
			  throw({forbidden: "Missing required properties"});
			}
		  }			  
		   else if(doc.type=='task')
		  {
			channel(doc._id);
			requireAccess(doc._id);
			access(doc.taskList.owner,doc._id);				
		  }			   
		   else if(doc.type=='task-list.user')
		  {
			access(doc.username,doc.taskList.id);
			requireUser(doc.taskList.owner);
			channel(doc.taskList.id+".users");				
		  }
		}`,			
	"shadow": {
             "server":"http://admin:test1234@127.0.0.1:8091",
             "bucket": "todo_test_bucket"
        }	
 }
}

}

This is my documents
1.
{
“name”: “Category”,
“owner”: “user1”,
“type”: “task-list”
}

{
“complete”: false,
“createdAt”: 1515491641236,
“task”: “Item”,
“type”: “task”,
“taskList”: {
“id”: “user1.30f8a492-64f9-c1b8-97fc-60f529e73c48”,
“owner”: “user1”
}
}

Thank q.

To access Sync Gateway while running in an emulator, you need to use the special address the emulator recognizes to connect to localhost (see https://stackoverflow.com/questions/5528850/how-to-connect-localhost-in-android-emulator?lq=1 for the standard Android emulator).

What versions of Couchbase Lite/Sync Gateway/Couchbase Server are you running?

And of course you need to have a replication active in your Android app.

1 Like

Curious - noticed that you are setting up shadow buckets . What version of platform are you running. Not sure if you are aware, but in Couchbase Server 5.0 / Sync Gateway 1.5, we have shared bucket access. Would it be an option for you to use Couchbase Server 5.0 / Sync Gateway 1.5 if you are not on that ?

Since you specifically mention “emulator”, just to clarify that if you have you sync function and sync gateway set up correctly, it should not matter if your Android app is in the emulator or device. It should sync regardless as long as your Android app is pointing to the sync gateway. I am assuming you are able to push up documents from the app. Use 10.0.2.2 for localhost access from emulator.

The formatting of your sync configuration file is messed up so hard to determine where you have specified the “shadow” property. make sure its at the top level of the config file.

1 Like

Thank q.
I am using
couchbase lite -> 1.3.1
sync gateway -> 1.5.1
couchbase server -> 5.0

Thank you. I am using couchbase lite -> 1.3.1, sync gateway -> 1.5.1, couchbase server -> 5.0 and php 7.1.9. I used url 10.0.2.2:4984/todo/ for localhost access from emulator.

Since you are on CBS 5.0 and SGW 1.5.1 can you use shared bucket access as discussed in the link I shared earlier? I would recommend moving away from shadow buckets as its being deprecated.

Remove the “shadow” from the config file and replace as follows

Specifically, from the link

Enabling shared bucket access is simple. You will have to specify a couple of new configuration options in Sync Gateway Config file
– enable_shared_bucket_access to true: This enables Sync Gateway to use XAttrs
– import_docs to continuous: This causes the Sync Gateway to automatically import documents added via the SDK

Deployment Note: In a non-accel SG deployment, only a single SG node should have import_docs=continuous to avoid duplicate processing of documents

Refer to this sample SGW config file for the Todo app that has XAttrs enabled.

This new shared bucket access feature will allow documents pushed into the CBS from your PHP app to sync to the mobile app (via SGW).

Also can you confirm that documents that you push from the Android app reach the couch base server ? the URL is correct.

1 Like

Thank q priya.rajagopal. Now it is working fine :smile: When i push document from the android app reach the CBS and when i updated document in CBS it reach the android app :+1: