1 - Setup
1-1 Create Node.js Project
Install Node.js LTS. Ditto does not support the "Current" release.
❯ mkdir ditto-example❯ cd ditto-example❯ npm init
Optional: Configure typescript
❯ npm i -g typescript
added 1 package, and audited 2 packages in 2s
found 0 vulnerabilities
❯ tsc --init
Created a new tsconfig.json ...
1-2 Install Ditto
See the installation page for more information about platform compatibility.
npm i @dittolive/ditto
1-3 Initialize Ditto
Ditto is implemented using Rust and is bridged to JavaScript over WASM. That
means that you must initialize ditto first using init()
as part of an async
context.
Create an index.ts
file:
import { init, Ditto } from '@dittolive/ditto'
async function main () { // Initialize the Ditto module await init()}
main()
Then, you can create a new Ditto instance. The ditto
instance must stay in
scope for the duration of the program, otherwise it will be garbage collected.
To prevent garbage collection, make sure ditto
is a global variable:
import { init, Ditto, Identity } from '@dittolive/ditto'
let ditto
async function main () { await init()
// Create a Ditto' context: ditto = new Ditto({ type: 'onlinePlayground', appID: 'YOUR_APP_ID', token: 'YOUR_TOKEN_HERE'})
ditto.startSync()}
main()
Now you're ready to sync data!
1-4 Run the program
If you're using typescript, you must create the JavaScript file by running the following command:
tsc
Then, you can execute the file:
node index.js