Our goal with Ditto was always to create a platform that allowed developers to seamlessly transport data to and from devices, with or even without the internet. So when we first created Ditto, we created a distributed database that would synchronize data changes behind the scenes in a mesh network. Today, it is used in airlines, restaurants, and IoT apps worldwide with fantastic user feedback. But at its core, developers had to work with database operations to move data between devices. While these database commands are speedy and give Ditto its offline-first capabilities, it prevents developers from building latency-sensitive applications. Thus, use cases such as real-time audio, game character coordinates, and robotic servo data streaming were impossible with our database APIs.
These use cases for streaming latency-sensitive packets required no disk persistence at all. So we imagined: what if we could give developers lower-level access to just the mesh networking layer, bypassing our storage APIs? To that end, we've surfaced a new namespace with new functions on the Ditto object and called it "Bus." The Ditto Bus takes heavy inspiration from a Laminar, an open-source networking project designed to work with a game engine. We found its simplicity both elegant and intuitive to build robust applications.
How will I use this?
Note: the following section is subject to change as the API evolves over multiple versions. Code samples are generic and do not reflect the final API.
The Ditto Bus API is straightforward. It allows your application to send arbitrary bytes to a `DittoRemotePeer`. That means you'll want to use our `observePeers` API to see which devices are connected. You can safely store these values outside of the peer observer closure since they're just value types.
var peers = []
const peersObserver = ditto.observePeers { remotePeers in
peers = remotePeers;
}
You can send arbitrary packets of bytes to remote peers in two ways:
- Unreliable - Where packets can be dropped, duplicated, or received out of order. This close to a UDP-like experience where sending packets as fast as possible is important.
- Unreliable Sequenced - Where packets can be dropped, duplicated but never received out of sequence. Sequenced packets are very useful for applications that only want bytes to go forward from the sending time.
- Reliable - Where packets can't be dropped, are never duplicated, and are always received in order. This is most like a TCP API.
To send a packet to a remote peer use the `send` method.
ditto.bus.send(reliability: .unreliable, remotePeers: remotePeers[0..<2], bytes: [0xb0, 0x3d, 0xe1])
To receive bytes listen to the bus for incoming packets with the `onPacketReceived`
var busListener = ditto.bus.onPacketReceived((packet) => {
console.log(packet.bytes)
console.log(packet.fromRemotePeer)
})
And that's it! The Ditto Bus allows your application to tap into the raw connection system that powers the store APIs.
When can I use the Ditto Bus?
Unlike our previous API releases, we will take a different approach with the Ditto Bus by releasing it incrementally across different platforms. For now, the APIs will be available in the next release of DittoSwift for version 1.0.20. We decided to take this approach to give us more time to get feedback from a subset of our customer base. In addition, we anticipate that our API will need additional considerations for each platform, like Android and JavaScript.
Looking Ahead
The Ditto Bus is an incredibly powerful tool that comes with our SDKs. While the API is simple, we know that power users will want more options and configurations to fine tune their experience. We have some hints as to how the API will evolve in certain areas like setting congestion control options, listening to heartbeats, and fragmentation. In our first versions, Ditto will come preconfigured with many of these settings keeping the API simple so we that can focus on stability and general performance. Power users will be delighted to see more and more options as the Bus API evolves.
What about the Web Browser?
One limitation of the web browser is it's inability to send and receive data from unreliable protocols or establish remote peers without the help of a server. This makes it quite challenging to build extremely fast network-backed applications. Since WebSockets and HTTP APIs are all TCP under-the-hood, the overhead of acking can be a challenge to performance. However, browsers do all support WebRTC, which powers video and audio communication apps. The Ditto team is excited to evaluate if WebRTC can offer the same benefits of unreliable, lossy, high-speed performance in the browser like mobile, desktop, and IoT platforms.
Can I get Early Access?
While most of our core development of the Ditto Bus is complete, we have not yet surfaced the API to all platforms. If you're interested in prototyping something on the Ditto Bus, please contact us. We absolutely love working with customers who love to push boundaries.