Sync Gateway Backend Processing in .NET

I am trying to understand how a backend running .NET written in C# can access all data for a Sync Gateway / Couchbase Lite database. The backend will listen to changes in data, do processing on the server, and save data to the database. This data might later be consumed by mobile clients.

The Couchbase Server bucket holding a Sync Gateway / Couchbase Lite database is private to Sync Gateway: Data will have Sync Gateway-specific metadata attached, and it is assumed that no other processes will modify the data. Therefore, I probably need to set up a Couchbase Lite .NET database on the server, and access this from C#. I cannot use the Couchbase .NET SDK to access the data, as this library only works against Couchbase Server, not Couchbase Lite.

Will I really need to run Couchbase Lite .NET on the server, connecting to Sync Gateway?

Which SDK do I need to use in C# to access the Couchbase Lite database? Is there a library supported by Couchbase?

I thrust the backend Couchbase Server will be able to handle large datasets. But what about my Couchbase Lite .NET database on the server? If I sync all data for all users to this database, I will face limits much lower than Couchbase Server gives me. Therefore, I guess I somehow need several servers, that partition which data they sync to Couchbase Lite .NET. Is this correct?

To me it looks like Couchbase Server should incorporate the concepts found in Sync Gateway, so that mobile clients sync directly with Couchbase Server. The data in Couchbase Server should then be possible to modify directly without going through a sync with a Couchbase Lite instance.

Hi @Runar,

You would need an instance of Couchbase Server to connect with Couchbase Lite & Sync Gateway.
Couchbase Lite would be embedded in your mobile app.
The platform support for Couchbase Server is listed in our Downloads page

You wouldnā€™t necessarily need several servers. Accessing data and partitioning data on the mobile app is executed through the ā€˜Channelsā€™ feature on the Sync Gateway side. The data access rules are defined using the Sync Function which is the heart of Sync Gateway. Can read more on how to integrate Channels specifics over here

A great starting point is the Mobile Developer portal and the section on Couchbase Lite with .NET and WPF specifically.

Hope that helps
William

I am still a bit puzzledā€¦ My first and second questions are really about how I can access data from a .NET backend that is hosted together with the Couchbase server. Since the Couchbase bucket holding data for the Sync Gateway is private to the Sync Gateway, I cannot access that bucket directly from the .NET backend. I guess the only option is to access a server-side Couchbase Lite .NET database, which syncs from the bucket, but I am not sure. Does anyone know?

My third question is about scalability. What if the aggregated data of all users gets largeā€¦: Of course, the backend Couchbase bucket can handle itā€¦ If I am correct, I would be able to let client Couchbase Lite instances sync only part of the data, so this is also not a problem. But what about my .NET backend? Will I need to ā€œshardā€ data to multiple Couchbase Lite .NET databases on the server, in order to have access to all data from the .NET backend?

The bucket is ā€œprivateā€ in the sense that you should not modify it, but reading from it will not cause a problem for your backend. In fact you can read from the bucket using the Sync Gateway REST API (which you can get at through any number of C# classes such as HttpWebRequest), or request some logic to be triggered on each sync gateway write. Iā€™d recommend this approach over reading directly from the Couchbase bucket since Sync Gateway already knows how to interpret what is in the bucket.

Thank you so much for answering!

I still need to update data from the backend, @borrrden. Since I cannot write to Couchbase views, and since I cannot write to the Couchbase bucket, will I need to replicate the data I intend to modify into a Couchbase Lite database hosted at the backend?

You can write anything you like to the bucket as long as it is through Sync Gateway. Perhaps you could give an example of the kind of data format you will receive and need to insert in the backend system.

Thank you again! I will look more into it.

Hi,

I have the same scenario.

I have a web application that I want my users to use to update some of the documents synced to the mobiles.

I have tried shadowing and updating the documents in the shadow database but it creates inconsistency in the database. The changes stop getting propagated. This is also given in the sync gateway as not advised. I think it is wise to think this wont work.

Given all this, I am not sure how easy the REST API going to be compared to the .NET SDK. In the .NET SDK we have support for strong types using dynamic types for document reading which is way easier in parsing the document compared to the navigation using JSON reader.

I strong feel, the .NET SDK should be made to work with the gateway REST API.

1 Like

I am considering reading thru the shadow bucket using .net api and updates thru sync gateway REST API.

But this is really an indirect approach.

1 Like

Just to update this thread, sync Gateway 1.5 and Couchbase 5 now allows to modify data on the server without mess sync Gateway.

And here is a related blog post on shared bucket access in Couchbase Mobile 1.5

Nobody is really answering his question:
He wants a back-end API to access Sync Gateway without the performance limitations imposed by going through a Couchbase Lite intermediary like this :
His Back-End > Couchbase Lite > Sync Gateway > Couchbase Server > Sync Gateway > Couchbase Lite > Device.

Since Couchbase 5.0 and Couchbase Mobile 1.5 shared bucket access allows him to use the SDK to do this, like this:
His Back-End > Couchbase Server > Sync Gateway > Couchbase Lite > Device.
However it still doesnā€™t allow access to features such as attachments to Sync Gateway.

So to answer his question, he needs to cut out Couchbase Lite and directly access Sync Gateway via the Admin REST API or the Public REST API here : https://developer.couchbase.com/documentation/mobile/1.5/references/sync-gateway/admin-rest-api/index.html
Yielding this:
His Back-End >Sync Gateway (Admin REST API) > Couchbase Server > Sync Gateway > Couchbase Lite > Device.

Hope that helps someone, because that is what we are having to do. If someone has an easier way please let me know! Thanks

Can you clarify what part of whose question was not answered? There are multiple questions here which go back to 2 years and the most recent response (which is our recommended approach) is to use shared bucket access to synchronize server side and mobile side changes.

The rationale for shared bucket access is to avoid the need to use the REST API for server side changes. So this is not required with latest release

What do you mean by this? Can you please give an instance where this is not working for you

Hi @priya.rajagopal ,
Yes you are correct - the most recent answer addresses it partially by mentioning shared bucket access available in the latest versions of Couchbase and Couchbase Mobile. However Attachments are not a construct of Couchbase from what Iā€™ve gathered, so that funcitonality/data does not get addressed by the shared bucket access.
The only reference I could find was this forum: Storing attachments directly on Couchbase Server where ldoguin specifically mentions one still needs to go through the Sync Gateway to interact with Attachments.

Example:
Server backend with a document in Couchbase. I want to add or modify an attachment to the document. Iā€™m using the .Net SDK -> Canā€™t except with Sync Gateway Rest API.
Server backend, with a document in Couchbase that has attachments. I want to get the attachment -> Also canā€™t except with Sync Gateway Rest API.

It is totally possible that I have missed something and there is a way (Iā€™m new to Couchbase), but to date I havenā€™t found any other way except adding a Couchbase Lite client on the server itself, which is not ideal.

I donā€™t want to hijack this thread, although this seems quite relevant - let me know if I should start a new one. Thanks!

Can you re-post this in a separate thread please. Thanks