Knowledge Base
Migration Guide for Atlas Device Sync
introduction this guide provides a comprehensive roadmap for migrating from mongodb atlas device sync to ditto by making this transition, you’ll benefit from resilient data synchronization, offline first capabilities, and efficient data management for a consistent user experience in any environment why ditto? ditto offers a decentralized solution for faster, more efficient data synchronization at the edge unlike traditional cloud based systems, ditto provides an offline first architecture that ensures your app continues to operate smoothly even when internet connectivity is limited or unavailable we take offline functionality one step further by utilizing peer to peer synchronization and mesh networking these core capabilities enable your applications to not just operate smoothly in any network environment but sync in real time without reliance on wifi, servers, or the cloud with just your existing mobile and edge devices, ditto unlocks, self organizing mesh networking that automatically and securely discovers nearby devices to form wireless networks real time peer to peer data sync within the mesh via bluetooth low energy, peer to peer wi fi, and local area network, plus opportunistically with the cloud offline first capabilities , enabling uninterrupted functionality without reliance on network hardware or cloud services efficient conflict resolution optimized to sync only the deltas, ensuring low bandwidth usage and enabling concurrent edits improved latency and performance by cutting out round trips to the cloud and enabling direct device to device communication please review this guide thoroughly to understand the necessary requirements and changes by following the outlined steps, your team can successfully migrate to a more robust solution with minimal disruptions if you need further assistance at any point, feel free to https //docs ditto live/support/contact us explore our compatibility guides ditto works across a wide range of platforms, programming languages, and device types, allowing for flexible real time syncing in many environments whether you’re building for ios, android, or desktop environments, or integrating across devices using different transport methods like bluetooth or wi fi, ditto supports it before migrating, it’s important to review the compatibility guide to ensure you’re familiar with all the platforms and languages we support this will help you understand the various options available for your project and ensure your app can take full advantage of ditto’s peer to peer capabilities https //docs ditto live/compatibility → connecting to mongodb ditto lets your apps sync directly between clients (small peers) and a big peer ( https //docs ditto live/cloud ) for real time data sharing, with an offline first design this setup reduces the need for constant cloud access and improves performance t his guide will help you replace realm on the client side to start using ditto’s peer to peer syncing right away the integration between ditto's big peer and mongodb is enabled by the docid foc3fxgel5hxqxhetb4o , which synchronizes data bi directionally between ditto and mongodb unlike other data synchronization systems for mongodb, ditto's advanced conflict resolution (based on crdts) avoids the need to deploy extra backend services to handle writes and conflicts, just deploy a big peer and you have everything you need to integrate with mongodb you can sign up for the https //ditto live/platform/mongodb connector waitlist to request access to the preview! migration steps 1\ get started sign up and create your ditto app to get started with ditto, sign up for an account and create a new project in the ditto dashboard you’ll need to grab your api keys to integrate ditto with your app for real time syncing sign up for ditto create an account on https //ditto live create an app obtain your app id and playground token, in the https //portal ditto live 2\ remove realm before adding ditto, we advise you remove mongodb realm from your project this includes uninstalling the realm sdk and clearing out any realm related code and dependencies removing realm at this stage will let you identify all parts of your codebase touching realm that will need to be migrated to ditto with the compiler it is also possible to postpone this until later in the process, and effectively run realm and ditto in parallel, if desired 3\ install ditto now that realm is removed, it’s time to install ditto follow our installation guides to add ditto to your project be sure to configure the right permissions for your app, like background mode and local network access visit our https //docs ditto live/install guides for instructions specific to your platform https //docs ditto live/install guides/swift#kx je 4\ initialize ditto next, you’ll set up ditto in your app by initializing the ditto service with your api keys this will allow your app to start syncing data between devices using ditto’s real time peer to peer capabilities let ditto = ditto(identity onlineplayground(appid "your ditto app id", token "your playground token")) ditto startsync() kotlin private val ditto ditto? by lazy { try { val androiddependencies = defaultandroiddittodependencies(context) val identity = dittoidentity onlineplayground( androiddependencies, appid = "replace with your ditto app id", token = "replace with your ditto token" ) ditto(androiddependencies, identity) apply { startsync() } } catch (e exception) { log e(tag, e message orempty()) null } } ditto is distributed by default, meaning it will automatically connect to, and sync data across your nearby devices in order to solely rely on device to cloud sync, you can https //docs ditto live/sync/customizing transport configurations#oyuoq by turning off lan, peer to peer wi fi, and bluetooth transports 5\ update your data models with realm your models are tightly coupled to realm’s classes this is not required with ditto make sure to review your data types, relationships, and primary keys, and update them where necessary to work seamlessly with ditto’s peer to peer sync architecture before (realm model) final class todo object, objectkeyidentifiable { @persisted(primarykey true) var id objectid @persisted var title = "" @persisted var completed = false }class todo() realmobject { @primarykey var id objectid = objectid() var title string = "" var completed boolean = false } after (ditto model) final class todo codable { 	var id = uuid() var title = "" var iscompleted = false 	var isdeleted = false }data class todo( val id string, val title string, val iscompleted boolean = false, val isdeleted boolean = false ) there are a few conventions with ditto data models worth considering document id keys are called id and are usually uuids timestamps are strings in iso 8601 format ditto will either return items as map\<string, any> or as a json string, which you may consider deserializing as native data types we also recommend you make yourself familiar with https //docs ditto live/data types/register , https //docs ditto live/data types/map , and https //docs ditto live/data types/attachment data types read more about best practices for data modelling with ditto in https //docs ditto live/best practices/3 data modeling and sync logic 6\ set up subscriptions you can replace realm's sync logic with ditto subscriptions to keep data in sync between your devices in real time let query = "select from todos" var subscription = try ditto sync registersubscription(query query)val todossubscription = ditto sync registersubscription( query = "select from collection todos" ) in order for ditto to sync data between client devices and big peer you will need to register a subscription for each collection you would like to sync start your subscription as early in the application lifecycle as possible keep a reference to the created subscription, so you can manage it and it doesn’t get garbage collected 7\ migrate your data operations now, update the create, read, update, and delete (crud) operations in your app replace realm’s methods with ditto’s, ensuring all your data interactions are using ditto’s sync logic create when adding new items, use the execute method on the ditto store object to insert your data this ensures new records are synced efficiently across peers in real time func addtodo(title string) { let newtodo = todo(title title) ditto store execute(query "insert into todos documents (\ todo)", 	 arguments \["todo" newtodo]) }suspend fun addtodo(title string) { val newtodo = todo( id = uuid randomuuid() tostring(), title = title ) ditto store execute( query = "insert into todos documents (\ new)", arguments = mapof( 	 "new" to newtodo asmap() // here you would have some code to serialize your class as a map\<string, any> ) ) } read refactor how you retrieve data by leveraging ditto’s powerful observation capabilities when reading data in ditto, the primary method is to set up an https //docs ditto live/crud/observing data changes that listens for changes and updates in real time this ensures your app automatically reflects the latest data from other peers real time observing this allows your app to respond to data changes in real time by syncing updates across all connected devices private func observetodos() { return ditto store registerobserver( query "select from todos where isdeleted = \ isdeleted", 	 arguments \["isdeleted" false]) { results in 	// decode the results 	} }suspend fun observetodos() flow\<list\<todo>> { 	return ditto store registerobserverasflow( 	 query = "select from todos where isdeleted = \ isdeleted", 	 arguments = mapof( 	 "isdeleted" to false 	 ) 	 ) } one off reads in addition to real time observations, ditto allows for one time reads these are useful when you need to fetch data at a specific point in time without continuously listening for updates you can do this by using the execute method on the ditto store using the same query (for example “select from todos where isdeleted = false” ) update when updating data, use update on the ditto store object ditto lets you sync the minimum data needed, ensuring that all peers converge on the same version of the data across the network func update( todo todo) { ditto store execute( query "update todos set iscompleted = \ iscompleted where id = id", arguments \[" id" todo id, "iscompleted" !todo iscompleted] ) }suspend fun toggleiscomplete(todo todo) { dittostoremanager executequery( dittoquery = "update todos set completed = \ iscompleted where id = id", arguments = mapof( "iscompleted" to !todo completed, " id" to todo id ) ) } delete in a distributed data system like ditto, deletion follows a “ https //docs ditto live/crud/delete#bpsqw ” pattern this means that when you mark an item as deleted, it’s not immediately removed from the collection or local storage instead, the item is flagged as deleted to ensure that other devices in the network remain in sync to fully remove the item and free up space, you’ll need to evict these “soft deleted” items for more details, see the section on https //docs ditto live/crud/delete# blnb func delete( todo todo) { ditto store execute( query "update todos set isdeleted = \ isdeleted where id = id", arguments \[string any] = \[" id" todo id, "isdeleted" true] ) }suspend fun deletetodo(todo todo) { ditto store execute( dittoquery = """update todos 	 set isdeleted = \ isdeleted 	 where id = id""" arguments = mapof( "isdeleted" to true, " id" to todo id ) ) } 8\ ensure your views respond to updates your app needs to react to changes in real time data set up https //docs ditto live/crud/observing data changes to make sure your ui updates when the data synced through ditto changes 9\ evictions in distributed systems, it’s important to manage storage efficiently, especially when dealing with large datasets or limited device space evictions allow you to remove data that is no longer needed, freeing up space in a controlled way while maintaining the integrity of your app’s data to manage storage, ditto allows you to configure eviction policies this lets you free up space by removing data that is no longer needed or marked as deleted (as described in the soft delete section) for more on evictions and deletion, refer to ditto’s documentation https //docs ditto live/crud/delete 10\ authentication and authorization a production application must have robust auth system in place ditto lets you rebuild your atlas device sync authentication and authorization flow in this document docid\ v47zesjkjwmvh5nc7udy9 additional resources need assistance? https //docs ditto live/support/contact us ! check out our https //docs ditto live/best practices learn more about our https //ditto live/platform/mongodb connector coming soon