How? Mobile app to query/write directly in to CB or via SyncServer


#1

Am developing an Mobile application which uses couchbase.lite local db to store offline conentent and sync with CBServer using sync-gateway.

So when a new user comes in for registration, i will need to lookup directly on CBServer for already registered user id and also will need to write his user document in CBServer before he creates a local couchliteDB.
–What is the best way to access CBServer? Call CBServer directly or via Sync-Gateway?
– what is the libraries available for Xamarin / Android.


#2

No you won’t. This goes against the idea of multi master replication. If you are replicating data between the server and the device via sync gateway, then you will already have a copy of the data in your local database. DO NOT call to Couchbase server directly because you will cause trouble.

We have libraries for both Xamarin iOS (unified) and Android, but the iOS one has a known issue at the moment. 1.2.1 will fix it and will be out soon (this week I think).


#3

But to validate a new user registration, check for duplicate user name I don’t want to sync the whole list of user documents into my mobile local store… That’s why thought of making a one time directly…

How to address this problem without bringing in all data to local dB on mobile to save space n network traffic? Any other options available that am not aware?

Thanks…


#4

If you want to go that route, you will lose the offline benefit and the user will be forced online during registration. You can use the Sync Gateway REST API to create a document directly (or query it) and put it in a channel that does not get synced to the devices normally. This is a race condition in theory, but in practice I don’t think you will have two users registering at the same time from two different devices. If it were to happen somehow, you would receive a 409 response from Sync Gateway if you tried to upload a document that already exists. It is not recommended to expose your Couchbase Server instance to the outside Internet because it is designed to be in the backend (i.e. doesn’t have a lot of security around its CRUD operations) so do all of your operations either through sync gateway or an application server you develop.


#5

I think you should deploy a register server on the server side, and you can use Sync Gateway REST Admin API to get all user info by:

curl -X GET -H "Content-Type: application/json"  'http://127.0.0.1:4985/sgdb/_user'

by register server(you should ensure security for any call to register server).
That request will return the user list array that has been registed, such as.

["user1","user2","user3"]

#6

You’re talking about registering users on the server side, right? I think @borrrden was assuming you were going to do this on the client.

In your app server you can create a user account with a POST to /db/_user on the admin port, putting the user name in the name property of the JSON body (see the API docs). This will fail if the user name already exists.


#7

Thanks all for your reply, will try it.

Btwn to call REST API what client library are you using? Am writing mobile app using Xamarin for Android/iOS(yet to start)

for normal CRUD operation am using CBL 1.2.x, this has support only for native api, i dont see any support in this client for REST Api. Can you advice if there is any or should i use any of the raw http req/resp available?


#8

We’re not talking about doing this on the client side. You’d write server-side code using Node.js or Rails or whatever. It gets called by an HTTP request from the client, and it uses an HTTP library to send requests to the admin port of Sync Gateway.