Large File Sync Even Without Internet
Introduction
As the realms of business and personal life increasingly gravitate towards mobile platforms, the demand for real-time file sharing has become commonplace. Despite its widespread presence, building such an application is not as straightforward as it may seem. Supporting multiple operating systems and protocols makes the task more difficult, especially between Android and iOS devices. This blog post will delve into the complexities of building file transfer applications using two of the most popular technologies at our disposal: Peer-to-Peer Wi-Fi and Bluetooth.
To understand the limitations of the current state of these technologies, we must first discuss the nuances of both Android and iOS operating systems, as well as the distinct characteristics of Wi-Fi and Bluetooth. Peer-to-peer Wi-Fi and Bluetooth APIs are often chosen to implement file-sharing functionality due to their ubiquity and compatibility with a plethora of devices. However, implementing a file transfer mechanism using these technologies poses several challenges for developers.
Challenges
1. Distinct APIs
Each platform has distinct approaches to its Bluetooth and Wi-Fi APIs, yielding different sets of capabilities and limitations. For instance, in terms of Bluetooth connectivity, iOS only supports the Bluetooth Low Energy (BLE) profile, whereas Android supports both the classic Bluetooth and BLE profiles. Consequently, developers are often forced to juggle platform-specific implementations, which can be time-consuming and lead to inconsistencies in user experience across Android and iOS versions of applications.
2. Limitations of Peer-to-Peer Wi-Fi
Another primary obstacle is the limitation of peer-to-peer Wi-Fi. While this technology enables fast and reliable file transfers, its range is restricted, often requiring users to be in close proximity to one another for successful data transmission. Additionally, connecting to a Wi-Fi network can be a cumbersome process and may even be unavailable in certain circumstances.
3. Varied Platform Compatibility
Furthermore, each platform has varied compatibility with specific Wi-Fi and Bluetooth protocols. This can make it challenging to achieve seamless file transfer functionality across different devices since developers must cater to a wide range of protocols while maintaining a consistent user experience.
Owing to these evolving limitations and challenges, it is crucial to explore new solutions or technologies for file transfer between platforms.
In this blog post, we will outline the limitations of the current file transfer solutions utilizing peer-to-peer Wi-Fi and Bluetooth APIs, discuss the technical challenges faced by developers, and explore an alternative that addresses these issues.
How did things get so bad?
Over the years, operating system like Android and iOS have evolved separately, driven by goals of delivering unique experiences and features. As peer-to-peer wifi and Bluetooth were introduced, each platform implemented these technologies in ways that aligned with their respective objectives and design philosophies.
Android, with its focus on customization and openness, embraced a diverse range of protocols and hardware, iOS emphasized a closed ecosystem, prioritizing user privacy and security. This divergence between Android and iOS has led to distinct methods of implementing these wireless communication technologies, presenting challenges for interoperability and seamless file transfers between devices. These challenges highlight the urgency for a unified solution that can provide a standard implementation.
- Performance. Break up large files into smaller chunks and send them separately. There are many strategies with various trade-offs for implementing chunking, which can create significant complexity. Even with chunking, Bluetooth Low Energy can only transmit up to 20kb/sec, which means a 10 MB file would take around 8 minutes to transfer.
- Reliability in the face of interference and dropped connections. Additionally, it is important to handle exceptions and errors that may occur during the file transfer process. It can be complex to keep track of all failed attempts for each chunk of the file to ensure that the transfer is completed successfully.
- Cross-version and platform compatibility. Finally, be aware that some devices may have different implementations of Bluetooth file synchronization, so it is recommended to test the process on different devices to make sure that it works as expected.
- Accommodating multi-device file transfers. One limitation of Bluetooth file synchronization is that it typically supports the connection of only one device at a time. However, you can implement multiplexing to overcome this limitation and synchronize files over Bluetooth across multiple devices at once.
Ditto Makes Bluetooth Transfer Easy
Ditto enables a new way of implementing Bluetooth file synchronization that makes it simple to share files between devices, even without internet. Ditto maintains multiple offline transports such as P2P Wi-Fi, BLE, and LAN - what we call the Rainbow Connection. With the Ditto Rainbow connection, your app will automatically switch to the fastest transport available. This means that when devices come in closer proximity to each other, Ditto will intelligently switch to LAN or P2P Wi-Fi, without a single line of code.
To see this in action, simply download the Ditto Chat app on your Apple devices and follow the prompts to enable location. Once connected, you can easily share files between devices wirelessly, without the need for cables or a complicated setup.
Ditto makes it easy to integrate Bluetooth file synchronization into your app, with just a few lines of code. With its simple API, you can add powerful Bluetooth and P2P Wi-Fi file transfer capabilities to your app in no time.
Syncing a File over the Rainbow Connection with DittoSwift
For more information and how-to instructions, see the official Platform Manual > "Attachment" article.
Sending the file
let metadata = [
"filename": "\(userID)_avatar.png",
"createdOn": ISO8601DateFormatter().string(from: Date())
]
let attachment = collection.newAttachment(
path: imagePath,
metadata: metadata
)!
// add the attachment to the Alice user document
ditto.store["users"].findByID(user.id).update { mutableDoc in
mutableDoc?["avatarToken"].set(attachment)
}
For the full example code, see the DittoChat app's DittoService.swift code in GitHub.
Receiving the file
For a step-by-step tutorial on how to use Ditto's Attachment feature to fetch and use a file, see the official Ditto SDK Setup Guide for Swift > "Attachments: Chat App" tutorial.
self.fetcher = ditto.store[collection].fetchAttachment(token: token) { event in
switch event {
case .progress(let downloadedBytes, let totalBytes):
let percent = Double(downloadedBytes) / Double(totalBytes)
onProgress(percent)
case .completed(let attachment):
do {
let data = try attachment.getData()
if let uiImage = UIImage(data: data) {
onComplete(.success(uiImage))
}
} catch {
onComplete(.failure(error))
}
default:
print("Error: event case \(event) not handled")
}
}
}
For the full example, see the DemoChat app's MessageBubbleVM.swift code in GitHub.
Multihop
Additionally, Ditto uses change data capture (CDC) to synchronize images between your existing systems on the cloud, as well as between offline devices connected in the mesh by way of multihop replication. CDC is a technique that tracks and captures data changes in real-time. Multihop replication is a technique used to propagate data across the mesh by way of intermediate “hops” along a given path.
Given that Ditto uses the NOISE protocol for end-to-end encryption, you can rest assure that your data is protected from prying eyes.
Conclusion
In this tutorial, we have discussed how to get started with Bluetooth file synchronization and how to use Ditto to make the process even easier. We have provided code snippets and a video demo to help you understand how to implement Bluetooth file synchronization in your app.
While it is possible to implement Bluetooth file synchronization using low-level APIs, there are several problems that can arise, including performance issues, security concerns, reliability in the face of interference and dropped connections, compatibility issues, and the need to implement multiplexing to allow for multiple devices to be connected simultaneously. Ditto solves all of these problems with its simple API, automatic Rainbow connection, end-to-end encryption, built-in multiplexer, and the ability to switch to the fastest transport available.
Since Ditto handles the complexities associated with Bluetooth file synchronization, you can quickly and easily integrate this functionality into your app without the hassle of extensive development and troubleshooting work. This saves you time and resources, allowing you to prioritize the development of features that matter most to your users.
With its easy-to-use API, powerful features, and ability to work across different platforms and devices, Ditto is the best choice for integrating Bluetooth file synchronization into your app.