Get Started
Install Guides
React Native
this article provides instructions on how to add the ditto package to your project, import and initialize ditto, and prepare react native apps targeting ios and android for usage with the ditto sdk /#react native cli support docid\ yn1njzd48h myo90e857v /#integrating ditto /#configuring sync /#enabling smallpeerinfo feature /#setting up presence /#setting logs to debug level /#troubleshooting jsi bindings error react native cli support the ditto sdk is designed for apps built with bare react native cli apps created with expo specific configurations may not be fully compatible integrating ditto once you've confirmed that you have the necessary prerequisites, integrate ditto within your react native app set up a react native cli project ( /#creating your project ) install the ditto package into your project ( /#installing the ditto sdk ) install the project dependencies ( /#installing dependencies ) enable your app to sync with nearby devices ( /#configuring permissions ) import the previously installed ditto sdk package in your app ( /#initializing ditto ) set up permissions handling ( /#handling permissions ) set up authentication ( /#setting up authentication ) specify how ditto should handle sync ( /#creating a new instance of ditto ) set up transport configurations ( /#setting transport configurations ) initiate sync at global scope ( docid\ yn1njzd48h myo90e857v ) if desired, enable the small peer information feature ( /#enabling smallpeerinfo feature ) if desired, monitor the activity of remote peers in the mesh by logging relevant information in logs or using ditto ’s presence viewer app ( /#setting up presence ) if desired, set the log level to debug ( /#setting logs to debug level ) create your subscription logic ( docid\ yn1njzd48h myo90e857v ) creating your project first, set up your environment and initialize your react native cli project for instructions, see the official https //reactnative dev/docs/environment setup?guide=native from react native before you begin the react native setup procedure, make sure to select the steps for react native cli quickstart the ditto sdk is designed for apps built with bare react native cli and may not be fully compatible with react native apps built with expo go to verify that you’ve set up your environment correctly, install and run the react native cli doctor for more information, see the official https //reactnative dev/blog/2019/11/18/react native doctor blog post installing the ditto sdk from a terminal, navigate to the folder containing your react native project, and then, using yarn , run the ditto package yarn yarn add @dittolive/ditto npm npm install @dittolive/ditto while npm can also be used, yarn is the preferred package manager for react native projects installing dependencies from the root of your project, using either yarn or npm, install project dependencies based on the package json file yarn yarn install npm npm install configuring permissions once you've added dependencies, set up transports for the ios and android targets ios from xcode, enable bluetooth le and local network services in your app from your project's info plist file, add the following key value pairs tip to view info plist as source code, right click the file from the left sidebar in xcode > click open as > and then select source code from the menu once implemented, the following string values display to your end users as dismissable prompts explaining why the app requires certain permissions if your end users prefer a language other than english, replace the string values with their language equivalents \<?xml version="1 0" encoding="utf 8"?> \<!doctype plist public " //apple//dtd plist 1 0//en" "http //www apple com/dtds/propertylist 1 0 dtd"> \<plist version="1 0"> \<dict> \<key>nsbluetoothalwaysusagedescription\</key> \<string>uses bluetooth to connect and sync with nearby devices \</string> \<key>nslocalnetworkusagedescription\</key> \<string>uses wifi to connect and sync with nearby devices \</string> \<key>nsbonjourservices\</key> \<array> \<string> http alt tcp \</string> \</array> \<! your other keys > \</dict> \</plist> if deisred, customize the default values for the permission prompts by replacing them with your preferred text for example, if your end users prefer a language other than english, you can replace the default english strings with their language equivalents ensure your app continues to sync while it runs in the background by enabling bluetooth le background modes once enabled, your app continuously syncs in the background, even while the device is locked for official instructions, see https //developer apple com/documentation/xcode/configuring background execution modes from apple from the left sidebar, click to select your project click signing & capabilities click + capability and then, from the modal that appears, search and select background modes from targets , select your app from the list from background modes , toggle the following uses bluetooth le accessories acts as a bluetooth le accessory android from android studio, set up transport configurations for the android target update android's minimum sdk version to 24 or higher open the project level build gradle located in the android root directory set the minsdkversion to 24 for newer projects, set the sdk version in android/gradle properties add the ditto sdk as a dependency open the module level build gradle located in the android/app directory add the following line into the dependencies block located at the bottom of the file implementation group 'live ditto', name 'ditto', version '4 7 1' start the app in metro yarn start! if you receive the error message execution failed for task when starting the app in metro, add the following block to the module level build gradle located in the android/app directory if you encounter an issue, see /#troubleshooting jsi bindings error android { packagingoptions { pickfirst 'lib/x86/libjsi so' pickfirst 'lib/x86 64/libjsi so' pickfirst 'lib/armeabi v7a/libjsi so' pickfirst 'lib/arm64 v8a/libjsi so' } // rest of the file } initializing ditto within the top most scope of your app, import the ditto package with the necessary modules and components from the ditto sdk’s react native library import { ditto } from '@dittolive/ditto'; handling permissions within your default react component ( app ), use this helper function to request permissions if developing for the android target import {useeffect} from 'react'; export default function app() { async function requestpermissions() { const granted = await permissionsandroid requestmultiple(\[ permissionsandroid permissions bluetooth connect, permissionsandroid permissions bluetooth advertise, permissionsandroid permissions nearby wifi devices, permissionsandroid permissions bluetooth scan, ]); object entries(granted) foreach((\[permission, result]) => { if (result === permissionsandroid results granted) { console log(`${permission} granted`); } else { console log(`${permission} denied`); } }); } useeffect(() => { requestpermissions(); }, \[]); } configuring sync using an asynchronous function named startsync() , do the following define an identity configuration with authentication settings ( /#setting up authentication ) instantiate a ditto object with the specified identity ( /#creating a new instance of ditto ) configure peer to peer transport settings and enable transports ( /#setting transport configurations ) initiate sync ( docid\ yn1njzd48h myo90e857v ) setting up authentication create an identity object, define your authentication type , and then provide access credentials each instance of ditto running in your app must prove its identity to other peers to sync there are two types of identities you can use to authenticate onlineplayground — explore platform features and functionality without needing to set up your authentication mechanism onlinewithauthentication — if developing a production level app, integrate your own identity provider, such as auth0, identify users, give users read or write control, and integrate with your existing authentication systems for more information, see docid yd5xvjr9dasxkavmeyqb onlineplayground use an online playground identity for development you can find authentication details for your app in the https //portal ditto live use these to set up your identity import type { identity } from '@dittolive/ditto'; const identity identity = { type 'onlineplayground', appid '\<your app id>', token '\<your online playground token>', }; onlinewithauthentication use the online with authentication identity when developing a production app for deployment import type { identity } from '@dittolive/ditto'; const identity identity = { type 'onlinewithauthentication', appid '\<your app id>', authhandler { authenticationrequired(authenticator authenticator) { if (authenticator loginsupported) { authenticator loginwithtoken( 'will accept any password', 'auth webhook' ); console log('successfully logged in'); } }, authenticationexpiringsoon function ( authenticator authenticator ) void { authenticator loginwithtoken('will accept any password', 'auth webhook'); console log('successfully relogged'); }, }, enabledittocloudsync true, // enable to connect to ditto's cloud, defaults to true }; creating a new instance of ditto within a try statement, instantiate a new ditto object with the identity you’ve specified import { ditto, dittoerror } from '@dittolive/ditto'; let ditto ditto; try { ditto = new ditto(identity); } catch (error any) { if (error instanceof dittoerror) { // handle errors starting ditto } throw error } setting transport configurations using a transportconfig instance, do the following to set up transport configurations in your app c onfigure peer to peer transport settings so that all desired transport types available on the end user device are available to ditto s pecify how you want ditto to handle which transports to use to sync data across the mesh import { transportconfig } from '@dittolive/ditto'; const transportsconfig = new transportconfig(); transportsconfig peertopeer bluetoothle isenabled = true; transportsconfig peertopeer lan isenabled = true; transportsconfig peertopeer lan ismdnsenabled = true; // apple wireless direct link is only available on apple devices if (platform os === 'ios') { transportsconfig peertopeer awdl isenabled = true; } ditto settransportconfig(transportsconfig); constructing sync subscription logic create the logic that performs sync and data operations in your app, including registering subscriptions, executing store operations, and observing changes in the given collection for more information, see docid\ w4y7avicpmj0aowxmdvz2 and docid\ bem sphhfkspyp5zee36g for example, the following snippet defines a document object ( document ), creates a subscription for the cars collection, executes database operations and logs the results, registers an observer for watching changes in the cars collection, and, when changes are observed, updates the setdocuments and setloading state import type { documentid, queryresult, queryresultitem, } from "@dittolive/ditto"; const document = { id 987654, id 123131, model "cx 5", make "mazda", color "blue", }; ditto sync registersubscription(`select from cars`); async function runexecute() { const queryresult = await ditto store execute( "insert into cars documents (\ document) on id conflict do update", { document } ); console log(queryresult items map((item queryresultitem) => item value)); console log( "mutated", queryresult mutateddocumentids() map((docid documentid) => docid value) ); } runexecute(); ditto store registerobserver(`select from cars`, (response queryresult) => { const parseddocuments = response items map((doc queryresultitem) => { return doc value; }); setdocuments(parseddocuments); setloading(false); }); starting the sync process to start syncing with other peers in the mesh ditto startsync(); enabling smallpeerinfo feature if desired, gather and store peer to peer sync and network details automatically rather than needing to continuously query ditto for this data by enabling the smallpeerinfo feature once enabled, collected details are available within the ditto portal for more information, see docid\ ld343 2lgt0 3nkaw2qnr to enable smallpeerinfo collection ditto smallpeerinfo isenabled; ditto smallpeerinfo isenabled = true; await ditto smallpeerinfo getsyncscope(); await ditto smallpeerinfo setsyncscope('bigpeeronly'); ditto smallpeerinfo metadata = { test 'metadata' }; ditto smallpeerinfo metadata; ditto smallpeerinfo metadatajsonstring; setting up presence monitor remote peers in the logs or using ditto’s presence viewer app for more information, see the blog post “ https //ditto live/blog/ditto presence viewer ” to set up presence observations // this closure is called every time the mesh of // connected ditto peers changes ditto presence observe((graph) => { console log('peers ', graph remotepeers); }); setting logs to debug level capture database debug logs by setting the ditto log level to debug mode that way any potential issues are tracked to set the minimum log level to debug logger minimumloglevel = 'debug'; troubleshooting jsi bindings error when using the ditto sdk for react native in the ios simulator, you may encounter issues related to the jsi bindings not being installed for the dittornsdk module when using the hermes javascript engine to resolve the issue, modify your project settings and dependencies from the ios/podfile file in your project, set the minimum deployment target for ios platform \ ios, 14 from the ios/ directory in your project, update cocoapods dependencies pod install if you encounter a filesystem permissions error after updating cocoapods dependencies from xcode, disable the macos app sandbox for instructions, see the official https //developer apple com/documentation/xcode/configuring the macos app sandbox from apple using the sudo command, run your project with elevated privileges