What hooks does couchbase sync gateway provide for sync?

142 views Asked by At

Is it possible to use couchbase syny gateway in the following way:

1) Mobile client queries couchbase for data.

2) No data in couchbase present so this triggers a import of the needed data from for example a mysql database into couchbase.

3) The imported data is then transfered to the mobile client by couchbase synch gateway.

4) The mobile client goes to sleep.

5) After 12 hours of inactivity the data is removed from couchbase.

6) Next day the mobile client still holds the data offline and syncs again which sync gateway

7) the data is again imported to couchbase server and the diffs are synced with the client

Does couchbase provide hooks to implement such an flexable usecase? If yes could somebody point me to the important api calls?

Many Greetings

1

There are 1 answers

0
Hod On BEST ANSWER

The preferred way to do this would run most things through Sync Gateway (the data imports from the external source in particular should go through Sync Gateway, not directly to Couchbase, and removing the data should go through SG also.)

Sync Gateway's sync function runs when SG receives documents. In this sense, there's no way to trigger something based on nothing being there.

One way you might solve this is by having the mobile client push a special purpose document. Your sync function could catch this and react in several ways (fire a webhook request, start a replication, or you could set up something to monitor a changes feed and trigger from that).

Next you have the issue of removing the data on the Server side. Here the question is a little unclear. Typically applications write new revisions to SG, and these get synced to the client (and vice versa). If you remove everything on the Server side, you'll actually end up with what are called tombstone revisions showing the document as deleted. (This is a result of the flexible conflict resolution technique used by Couchbase Mobile. It uses multiversion concurrency control.)

The question is a little unclear. It sounds like you don't want to store the data long term on the Server side. If that's right, I think you could do something like:

  1. Delete the data (through SG)
  2. Have the mobile client push data to SG
  3. Trigger SG again with some special document
  4. Update the data from the external source
  5. Have the client pull updates from SG

That's a very rough outline. This is too complicated to really work out in this format. I suggest you post questions through the Couchbase developer forum to get more details.

So, the short answer, yes, this seems feasible, but a full answer needs more detail on what you're doing and what your constraints are.