Installing JavaScript SDK
You can integrate the Ditto SDK with your web and Node.js projects developed using JavaScript and TypeScript languages.
For a complete overview of the platforms, transports, and devices that the JavaScript SDK supports, see Compatibility with JavaScript.
If you're using Node.js and want instructions on how to create a task app, see the Node.js Task App Quickstart and the Task App: Node.js tutorial.
To install the JavaScript SDK:
Confirm that you meet the minimum requirements. (Prerequisites)
Install the ditto package in your environment. (Installing Dependencies)
Import and initialize Ditto in your app. (Integrating Ditto)
Authenticate with the Big Peer and then start syncing offline. (Integrating and Initializing)
Prerequisites
Following are the minimum requirements that must be met before attempting to install Ditto.
- Linux, macOS version 11, or Windows version 10.0
Installing Dependencies
Install the @dittolive/ditto package into your project:
npm install @dittolive/dittoIntegrating Ditto and Starting Sync
Import and initialize Ditto, and then provide your access credentials for one-time authentication with the Big Peer:
- From the top-most scope of your app, import Ditto.
- Using async await, initialize Ditto.
- Provide your access credentials:
- Replace the appID placeholder value with the app ID that identifies your app in Ditto.
- Replace the token placeholder value with your playground token that the Big Peer uses to verify your digital identity before issuing your playground certificate.
Make sure to instantiate the ditto object in the top-most scope of your app. Otherwise, it may be inadvertently garbage collected and no longer accessible throughout the lifecycle of your app.
For instructions on how to obtain your access credentials, see Onboarding. For an introduction to authentication in Ditto, see Ditto Basics > Authentication and Initialization.
import { init, Ditto } from "@dittolive/ditto";
let ditto;
async function main() {
await init()
ditto = new Ditto({
type: "onlinePlayground",
appID: "YOUR_APP_ID",
token: "YOUR_PLAYGROUND_TOKEN"
})
ditto.startSync()
}
main()Running Ditto
If you're using typescript, you must create the JavaScript file by running the following command:
tscThen, you can execute the file:
node index.jsSetting Up Subscriptions
You must create a subscription object in the global scope so that ditto synchronizes in the background throughout the duration of the program.
import { init, Ditto } from '@dittolive/ditto'
let ditto
let subscription
let liveQuery
async function main () {
await init()
ditto = new Ditto({ type: 'onlinePlayground', appID: 'YOUR_APP_ID', token: 'YOUR_TOKEN_HERE'})
ditto.startSync()
// Start a subscription
subscription = ditto.store.collection("tasks").find("isDeleted == false").subscribe()
}
main()Listening to Changes
Using the observeLocal method, as follows, for every change to a document, you can retrieve those documents and print them to the console:
For a step-by-step demonstration of how to create a basic task app, see the Task App: Node.js tutorial.
import { init, Ditto } from '@dittolive/ditto'
let ditto
let subscription
let liveQuery
async function main () {
await init()
ditto = new Ditto({ type: 'onlinePlayground', appID: 'YOUR_APP_ID', token: 'YOUR_TOKEN_HERE'})
ditto.startSync()
subscription = ditto.store.collection("tasks").find("isDeleted == false").subscribe()
// Listen for changes
liveQuery = ditto.store.collection("tasks").find("isDeleted == false").observeLocal((docs, event) => {
console.log(docs)
})
ditto.store.collection("tasks").upsert({
isCompleted: false,
isDeleted: false,
body: "Hello world!"
})
}
main()