It is essential that an application can continue to run smoothly even when an internet connection becomes spotty or drops altogether. For example, unreliable internet in a restaurant can lead to customer and employee frustration, as well as a significant loss in revenue. Most mobile applications today use an embedded database, like Couchbase Lite, to solve this problem by storing and processing data offline. While an embedded database is great for writing and reading from the local disk, it does not allow the application to synchronize any of the data changes in real time, directly between devices. Ditto solves this problem with an intelligent edge mesh network that synchronizes data over Bluetooth, LAN, or P2P WiFi. However, it can be costly and time-consuming to refactor a legacy application to use a different storage layer.
To solve this problem, Ditto now offers an API called the Ditto Bus. The Ditto Bus sends data directly between devices at incredible speeds over a low-latency streaming interface. In the video below you will see the Ditto Bus in action. This is a simple demo where the accelerometer is sending its current orientation to all of the other devices in the mesh.
Ditto Bus for Super Low Latency
Integrating Couchbase Lite and Ditto
Ditto Bus is a communication channel not tied to any particular storage system, which means you can add peer-to-peer offline sync to an existing app without changing the underlying database implementation. In this demo, we will add an offline mesh network to Couchbase Lite using the Ditto Bus.
The Moodlight app allows a user to select or randomize the color of the device screen. It's a very simple application that uses Couchbase Lite, but it is a good demonstration of how this integration works. You probably wouldn't want to use such a complicated database setup for a simple moodlight application, but we wanted to build something simple so we could focus on the logic that will connect the Ditto Bus and Couchbase Lite.
Here are 2 iOS devices and 2 Android devices synchronizing the moodlight's color over the Ditto Bus. Watch as the devices continue to synchronize changes in real-time without any connection to the internet.
Ditto Bus and Couchbase Lite Moodlight Demo
To see the code, you can access the open-source code here.
How it works
So how exactly do Couchbase Lite and Ditto Bus work together?
Couchbase Lite is an embedded, NoSQL JSON-document Style database. While you can use Couchbase Lite as a standalone embedded database, they also offer a sync interface that allows you to integrate your own custom listener. We're going to use this custom listener to leverage Ditto's mesh network for offline, peer-to-peer synchronization.
The Ditto Bus listens to the Couchbase stream and handles the data transfer between devices on the edge mesh network. Ditto Bus doesn't have any knowledge of the underlying stream of data: it only sends and receives bytes between peers. The Ditto Bus integration also works cross-platform, allowing apps to synchronize data between devices with different operating systems, such as iOS and Android.
When a new device advertises itself on the local peer-to-peer network, Ditto sends a callback to the application code. The application code then creates a Couchbase Lite P2P stream for each device.
Every time a color change occurs, that change is written locally to the Couchbase Lite database. Couchbase notifies the stream handler, which writes the resulting bytes to the Ditto Bus. The Ditto Bus then synchronizes those bytes (including the underlying Couchbase database change) to all other devices in the mesh network.
Using Ditto Bus
By bypassing storage and database APIs, you can now send arbitrary packets of bytes to remote peers faster than ever before. The Ditto Bus is ideal for use cases that are sensitive to latency, such as games, high-bandwidth multimedia like video and audio, IoT, manufacturing, robotic motors, or legacy databases. If you are interested in seeing how Ditto can be a solution for you, please contact us. We are happy to work in partnership to help develop your application.