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.4.0
Please visit ditto.live for more info 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 () => {
// Initialize the Ditto module
await init()
// Create a Ditto context:
const identity = { type: 'offlinePlayground', appID: 'live.ditto.playground') }
const ditto = new Ditto(identity, 'playground')
// Get hold of a collection:
const cars = ditto.store.collection('cars')
// Insert an entry:
const fordBlack = { _id: "ford-black-123", model: "Ford", color: "black" }
await cars.upsert(fordBlack)
// Find an entry by ID:
const foundFordBlack = await cars.findByID('ford-black-123')
console.log(foundFordBlack)
// Remove an entry:
await cars.findByID('ford-black-123').remove()
// Done:
console.log("Done, over and out.")
})()
NOTE: Ditto is powered by a
WebAssembly and native
Node core written in Rust under the hood. By default, when running in the
browser, 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 () => {
// Pass the URL to the ditto.wasm file:
await init({ webAssemblyModule: 'https://my-app/assets/ditto.wasm' })
// Then use Ditto as you normally would:
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.
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 © 2021 DittoLive Incorporated. All rights reserved.