Ditto JS SDK
Ditto is a cross-platform SDK that allows mobile, web, and IoT apps to sync
with and even without connectivity.
Version: 4.9.4
For more information please visit ditto.live, as well as the
API Reference for this particular version.
Getting Started
Add the Ditto NPM package to your project:
npm install --save @dittolive/ditto
Import @dittolive/ditto
in your source and start using it:
import { init, Ditto } from '@dittolive/ditto'
(async () => {
await init()
const identity = { type: 'offlinePlayground', appID: 'live.ditto.playground' }
const ditto = new Ditto(identity, 'playground')
const cars = ditto.store.collection('cars')
const fordBlack = { _id: 'ford-black-123', model: 'Ford', color: 'black' }
await cars.upsert(fordBlack)
const foundFordBlack = await cars.findByID('ford-black-123')
console.log(foundFordBlack)
await cars.findByID('ford-black-123').remove()
console.log('Done, over and out.')
})()
Browser Environments
When running in the browser and other web environments, Ditto is powered by a
WebAssembly core written
in Rust. By default, Ditto will fetch the corresponding WebAssembly file from
the web. That same file is contained within the NPM package under
web/ditto.wasm
so you can serve it yourself. Simply make the file accessible
somewhere on your server and pass the URL to init()
:
import { init, Ditto } from '@dittolive/ditto'
(async () => {
await init({ webAssemblyModule: 'https://my-app/assets/ditto.wasm' })
const identity = { type: 'offlinePlayground', appID: 'live.ditto.playground' }
const ditto = new Ditto(identity, 'playground')
})()
This may be useful for development or production environments without a reliable
internet connection. Make sure to properly configure the server to return the
correct MIME type (application/wasm
) and the
Access-Control-Allow-Origin
header if the .wasm
file is served from a different domain. For production
environments, make sure to use compression (gzip
) and proper caching headers.
React Native Environments
Using Ditto in React Native requires configuration of the native iOS and Android
projects to request network and location access permissions and to ensure
compatibility with the target environment. Please refer to our guide for
getting started with React
Native for more
information.
Playground
The NPM package has a built-in bare minimum playground web page (index.html
)
as well as a Node REPL ('playground.cjs') allowing you to start playing with the
Ditto SDK right away. Simply cd
into the package directory, run npm run play:web
or npm run play:node
and follow the on-screen instructions.
Copyright
Ditto JS SDK is a commercial product. Please consult LICENSE.md
within this
package for license details.
Copyright © 2023 DittoLive Incorporated. All rights reserved.