Peer to Peer sync via Wifi Direct or Bluetooth

Hi Team,
I know that documents are synced via sync gateway. But we have a peculiar problem and it is a long post (Sorry for that :sweat:). I recommend reading it completely and calmly :ice_cream: . We currently have an app which works as an offline first solution.

This app is developed using .Net, SQL server at server and client both. SQL server at server and SQL server express edition at local. We use tablets to run this app. These tablets have Windows.

Now the APP always get data from local DB (SQL server Express Edition) and data manipulations (Which can be done by using couch base lite). When the tablet is in online (reachable to server) there is a windows service sync data with server in background.This is done on multiple tablet in a near by area.

If in critical situations if connectivity is not available we have provided an option by which two tablets can sync their data via Bluetooth. One tablet sends data as a file and other receives it, process it and put data in its local DB.

When both tablets comes online conflict resolution is done and data is synced back to server. Right now we are doing it using Bluetooth but we do have plan to use WiFi direct.

Now my question is if we want to achieve this using Couch base lite and Andoid / IOS, how we can do this?

First solution comes to my mind is reading changed documents and sending then over Bluetooth/WiFi direct to other device as a file and then reading it and saving it with same revision number. but the question is whether it will confuse the sync gateway?

If we can have a better solution that I will highly appreciate your effort.

Couchbase Lite is its own database. It doesn’t work with external databases. If you’re already using Couchbase Lite, I’m confused as to how it connects with SQL Server. You must have written your own code to copy data between them?

In general it sounds like you’re trying to reimplement our replicator yourself using SQL Server and Bluetooth. Replication is a hard problem and very difficult to get right, and if you’re not using our technology for it I’m not sure how we can help you.

To restate this more positively: You can use Couchbase Lite’s replicator in peer-to-peer mode to sync changes between two devices without going through Sync Gateway. This won’t confuse the Gateway; the replication protocol is multi-master, meaning that changes can flow through an arbitrary topology of peers, not just in a traditional client/server star shape.

Hi @jens

Sorry for confusing you.

  1. We are planning to rewrite the app using Couch base Mobile So we will not be using SQL server or any other DB apart from couch base mobile framework. The explanation I wrote is about how does it work right now and how we have implemented it. And we want to move away from it and want to use a system which have built-in sync. So the question is how we will be able to do it using couch base mobile?

  2. [quote=“jens, post:3, topic:12219”]
    You can use Couchbase Lite’s replicator in peer-to-peer mode to sync changes between two devices without going through Sync Gateway
    [/quote]

If you can provide more information on this, May be some link then it will be help full. We will be using Android OR I-OS now on-wards.

We don’t have a turn-key P2P solution yet, but we provide the building blocks to do it.

  • When you start a replication you give it a URL for the remote database. This is simply an HTTP endpoint that speaks the REST-based replication protocol.
  • Couchbase Lite has an optional “Listener” component that runs a simple HTTP server, providing REST API access to your local databases. This is most commonly used to let an embedded web-app access the databases via XMLHTTPRequest, but if you bind the listener to a network interface (not just loopback) other devices can connect to it.
  • The REST API implemented by the listener includes the API methods used for replication.

Put this together, and if you enable the listener in Couchbase Lite on one device, then run CBL on another device and give the replicator a URL that points to the endpoint being served by the first device, the two peers can replicate (in either or both directions.)

The replication protocol is multi-master, so multiple peers can replicate with each other, and the topology doesn’t have to be a star. You can have an arbitrary directed graph, and updates will flow across it.

In practice, the missing pieces you need to implement are mostly about discovery and simplifying the topology.

Discovery means how the peers on the network find each other. To construct the URL to replicate with a peer, you need to know its IP address or hostname (and the database name, which is usually hardcoded.) Peers will tend to come and go dynamically without fixed addresses. Most often you’d use zeroconf discovery (mDNS and DNS-SD), which Apple calls “Bonjour”, for this. It’s obviously well supported on iOS and macOS, but it’s also in Android under a different brand name, and there are versions available for Linux (Avahi) and for Windows (don’t recall the name.)

At a small scale you can just have every peer replicate with every other peer, but the amount of traffic on the LAN increases as O(n^2), so this doesn’t scale. With larger numbers of peers you want to prune the connection graph to something more like a spanning tree, which requires some tricky distributed algorithms. I’ve never tried to implement this, but there is a lot of literature about it if you search for “mesh networks” or “ad-hoc networking”.

A few customers of ours have built systems like this. We’d love to have a turn-key system of our own, but we just haven’t had the resources to build it yet.

1 Like

I have found a blog from couch base which has already done this using IOS. Following is the blog URL for everyone’s reference :- https://blog.couchbase.com/photodrop/
The code for this app can be found at :- https://github.com/couchbaselabs/photo-drop

I am still looking for an andorid sample. Will keep updated once found.

We successfully implemented it using android as suggested by @jens. Thanks sir.

There are some question from my side as always :-

  1. I have read somewhere (May be in Github at the wiki of 2.0) that the Blip protocol you are designing will support Peer to Peer sync using bluetooth. Will it support for all platforms?

  2. Right now web socket is not available in Android. Is it a correct statement. If yes then will it be available in 2.0.

  3. Why there is a big difference in pull replicator performance between Android and Ios?

Thanks in advance.

Regards
Pankaj Sharma

I have read somewhere (May be in Github at the wiki of 2.0) that the Blip protocol you are designing will support Peer to Peer sync using bluetooth. Will it support for all platforms?

It’s capable of running over Bluetooth. We have no implementation of that currently.

Right now web socket is not available in Android. Is it a correct statement. If yes then will it be available in 2.0.

I can’t speak for Android 1.x, but the replicator in 2.0 uses WebSockets on all platforms.

Why there is a big difference in pull replicator performance between Android and Ios?

Likely due to the overhead of a Java implementation vs. native code.

Hi @pankaj.sharma
quote=“pankaj.sharma, post:7, topic:12219”]
We successfully implemented it using android as suggested by @jens. Thanks sir.
[/quote]

Curious: Which version of CBM did you implement this on (Assuming you were able to locate the Android version of the photoDrop app)? Did you use Android NSD for peer discovery?

I was not able to locate Android photoDrop App. So We used NSD for Peer discovery and made hot spot on one serving device and client on the other. Then we exchanged the URL and its done.

So this didn’t work ? https://github.com/couchbaselabs/photo-drop/tree/master/android. And NSD does seem to be a popular choice for peer discovery on Android

I had stopped searching for it. Hence haven’t given look at https://github.com/couchbaselabs/photo-drop/tree/master/android . Will check and let you know.

1 Like

Do you know what can I do for resfresh my new data I insert in another activity, but in my activity principal its null. (Android)