Crate dittolive_ditto

source ·
Expand description

What is Ditto?

Ditto is a cross-platform peer-to-peer database that allows apps to sync with and even without internet connectivity.

Install Ditto into your application, then use the APIs to read and write data into its storage system, and it will then automatically sync any changes to other devices.

Unlike other synchronization solutions, Ditto is designed for “peer-to-peer” synchronization where it can directly communicate with other devices even without an Internet connection.

In addition, Ditto automatically manages the complexity of using multiple network transports, like Bluetooth, P2P Wi-Fi, and Local Area Network, to find and connect to other devices and then synchronize any changes.

How to Ditto

First, you need to create a Ditto instance using the DittoBuilder :

use dittolive_ditto::prelude::*;
// 👇 See <https://docs.ditto.live/onboarding> for instructions on obtaining your app ID and
//    playground token.
let app_id = AppId::from_env("YOUR_APP_ID_VAR").unwrap();
let shared_token = "YOUR-PLAYGROUND-TOKEN-HERE".to_string();
let cloud_sync = true;
let custom_auth_url = None;
let ditto = Ditto::builder()
    .with_root(Arc::new(
        // 👇 folder where Ditto will keep its persistent data
        PersistentRoot::from_current_exe().expect("Invalid Ditto Root"),
    ))
    // 👇 this Identity is used to start developing features using Ditto
    .with_identity(|ditto_root| {
        identity::OnlinePlayground::new(
            ditto_root,
            app_id,
            shared_token,
            cloud_sync,
            custom_auth_url,
        )
    })
    .unwrap()
    .build()
    .unwrap();

Then, create a Collection in the Store.

let store = ditto.store();
let collection = store.collection("fancy_name");

And finally add some content inside, which we name Document.

use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Car {
    color: String,
    price: u32,
}
let ford = Car {
    color: String::from("black"),
    price: 42_000,
};
//   👇 This is the `DocumentId` which will allow you to edit the Document later
let ford_id = collection.upsert(ford);

Modules

  • Provides access to authentication information and methods for logging on to Ditto Cloud. Relevant when using an OnlineWithAuthentication identity.
  • Monitor disk usage on a device
  • Entry point for the DittoSDK
  • Ditto Error Types
  • Ditto Root Directory Wrapper
  • Ditto needs an Identity to start syncing with other peers.
  • Contains the observer pattern, used to subscribe to updates on Peers, Collections, and the file system.
  • The prelude contains all common imports when using the dittolive crate.
  • Store is a holder to create, read, write, and remove Documents from a Ditto peer.
  • Subscriptions are used to register interest in receiving updates for specified DittoDocuments.
  • Manage the synchronization properties of your Ditto instance from here.
  • Configure the transports for of your Ditto instance.
  • The Ditto custom types.