Sharing of POC : Syncgateway 1.5 and couchabase lite 2.0, Xamarin.Forms, .NET

Hi,

I just wanted to share my P.O.C. which is beginning to sync :
Here is my sync gateway json config file :

{
"databases": 	
{
	"travel-sample": 
	{
		"server": "http://localhost:8091",
		"bucket": "travel-sample",
		"username": "Gateway",
		"password": "production",
		"users": 
		{
			"Gateway": 
			{
				"disabled": false, 
				"admin_channels": ["*"], 
				"password":"production", 
				"name":"Gateway"
			}
		},
		"import_docs":true,
		"unsupported": {
			"replicator_2": true
		  },
		"sync": `function (doc, oldDoc) {
			if (doc.sdk) {
				channel(doc.sdk);
			}
		}`
	}
}

}

Please note this 2 importants options :

  • “import_docs”:true, to replicate all document which was in the server bucket, when you start sync gateway
  • “unsupported”: {
    “replicator_2”: true
    }, to be compatible with couchebase 2.0

Into my .Net solution, I create a Xamarin.Forms Shared Librery project, and added a .Net standard library to manage the link with couchbase Lite.

I created a class, couchbaseHelper with this functions :

public Database GiveBase()
    {
        string databaseName = "travel-sample";

        DatabaseConfiguration config = new DatabaseConfiguration();
        config.ConflictResolver = new ExampleConflictResolver();
        return new Database(databaseName, config);
    }

 public Replicator StartReplicator(Database db)
    {
        Uri target = new Uri("blip://localhost:4984/travel-sample");
        ReplicatorConfiguration replicationConfig = new ReplicatorConfiguration(db, target);
        replicationConfig.Continuous = true;
        replicationConfig.ReplicatorType = ReplicatorType.PushAndPull;
        replicationConfig.Authenticator = new BasicAuthenticator("Gateway", "production");
        var replication = new Replicator(replicationConfig);
        replication.Start();

        return replication;
    }

From my mainPage, which is located into the xamarin shared project, I created this 2 functions :

public void StartBase()
    {
        _database = new CouchbaseHelper().GiveBase();
        if (_database != null)
        {
            _replicator = new CouchbaseHelper().StartReplicator(_database);
            _replicator.StatusChanged += _replicator_StatusChanged;
        }
        
        BindingContext = this;
    }

public void CloseDataBase()
    {
        _replicator.Stop();
        _database.Dispose();
    }

This 2 functions are called from the App class like that :

public partial class App : Application
{
	public App ()
	{
		InitializeComponent();

		MainPage = new CouchBaseT2.MainPage();
	}

	protected override void OnStart ()
	{
        (MainPage as CouchBaseT2.MainPage).StartBase();
    }

    protected override void OnSleep ()
	{
        (MainPage as CouchBaseT2.MainPage).CloseDataBase();
    }

	protected override void OnResume ()
	{
        Console.WriteLine("App OnResume");
    }
}

I hope this could help some of you.

Regards

Steeve

PS : My confilct resolver

public class ExampleConflictResolver : IConflictResolver
{
    public ExampleConflictResolver()
    {

    }

    public ReadOnlyDocument Resolve(Conflict conflict)
    {
        var baseProperties = conflict.Base;
        var mine = conflict.Mine;
        var theirs = conflict.Theirs;
        

        return theirs;
    }
}

Hey @steeve.descarpentrie, are you running Couchbase Server 5.0 or later?

If so, you probably want

“enable_shared_bucket_access”: true,

rather than

“import_docs”:true,

See Couchbase Capella for Mobile Developers for more info.

That’s awesome that you’re posting your progress!

Also if you use “enable_shared_bucket_access”: true, you will need to change to "import_docs": "continuous"

To add to Traun’s links, you may want to take a look at this blog post which discusses the same

Hi guys,

Thank you for your helps, I will trythat , an read, and try again … :smile:

I’ll be back …

Regards,

Steeve