Last week I was speaking at WebCamp Zagreb, a great conference for developers and designers. I gave a presentation entitled ‘Let your devices talk to each other’. For the demo part I did a very simple message app that syncs automatically with anyone else who is running it on the same network. Slides and demo code are on github. The presentation has been filmed and should be online fairly soon.

The topic of this presentation is IoT (Internet of Things), M2M (Machine to Machine) and decentralisation. All these things of the internet are usually sending their data (most of the time very private data, yes I am looking at you Quantified self movement) to a server somewhere in the cloud. That basically means it’s not your (private) data anymore. This is very sad. This is the main idea that drove me to do this talk. But there are plenty of other good reasons to let your devices talk to each other. And this is not a new thing. What we call M2M now has been very common in the industry for years.

But let’s go back to the original topic. One way to avoid this centralisation would be that your things of the internet stop being things of the internet and start being things of your home network instead. They should start communicating with each others and send their data where you decide, like a Couchbase Lite instance running on a RaspberryPI or any other small device supporting the JVM.

And of course one way of making devices talk to each other can be to install Couchbase Lite on these and use P2P sync. We already have several blog post on the matter. So to bring something new to this I’ll tell you how to make that sync automatic.

Syncing is based on replication links. They usually requires an address and credentials. As I want as less friction as possible, I have skipped the credential/security part. Which is bad but made my presentation demo so much easier. So if all you need is an address, how do you get it automatically?

For P2P sync to work, devices syncing have to be on the same network. And the good news is there are several network protocol existing to do automatic service discovery. You might have heard of thinks like Bonjour, RendezVous, ZeroConf, Android NSD, mDNS, DLNA, UPnP and more. Most of them are based on the DNS service registry. Because yes the DNS can do more than map IPs to hosts. It can also store a list of service with their address, name and description.

This is what I have been using in my demo. I have chosen a simple Java implementation based on the JmDNS library. This way I have almost the same impl on my Java native app and on Android. It works by doing UDP broadcasting. It sends packet to other connected things on the network

Here’s a short look at the most relevant bits of code.

Starting Couchbase Lite Listener on the device to make it able to receive connections.

Exposing a new service using JmDNS

Listening to a particular service using JmDNS. The DiscoveryListener listener is important because it’s where you can actually setup the sync.

This is not the cleanest implementation there is but you’ll get the idea. Take a particular look at the serviceResolved method. It’s the one that is called when a new Service has been discovered on the network. It gives us the URL of the service thus allowing us to configure the classic replication link.

And that is a fun experimental project, easy to implement, to make sure all your Couchbase Lite devices can sync easily. There is of course much more that can be done. Like adding the security part. You could imagine having a first manual sync that exchanges credentials once and then do everything else automatically. I hope you’ll find this useful. Don’t hesitate to comment and give us feedback!

Author

Posted by Laurent Doguin, Developer Advocate, Couchbase

Laurent is a Paris based Developer Advocate where he focuses on helping Java developers and the French community. He writes code in Java and blog posts in Markdown. Prior to joining Couchbase he was Nuxeo’s community liaison where he devoted his time and expertise to helping the entire Nuxeo Community become more active and efficient.

Leave a reply