@dittolive/ditto
TypeScript icon, indicating that this package has built-in type declarations

4.7.1 • Public • Published

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.7.1

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 () => {
  // 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.')
})()

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 () => {
  // 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.

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.

Package Sidebar

Install

npm i @dittolive/ditto

Homepage

ditto.live

Weekly Downloads

1,149

Version

4.7.1

License

SEE LICENSE IN LICENSE.md

Unpacked Size

380 MB

Total Files

157

Last publish

Collaborators

  • pvditto
  • teodor.ditto
  • phatblat
  • craigatallahfrost
  • aaronleopold
  • jreyes33
  • mbalex99
  • hamditto
  • bigfish24
  • connorpower
  • konstantinbe
  • kndoshn
  • sinhditto
  • dittolivetester