JavaScript Web
You can integrate the Ditto SDK with your web and Node.js projects developed using JavaScript and TypeScript languages.
To install the JavaScript SDK:
Prerequisites: Confirm that you meet the minimum requirements.
Install Dependencies: Add the ditto package to your environment.
Integrate Ditto: Import and initialize Ditto in your app.
Run Ditto: Authenticate with the Big Peer and start syncing offline.
Prerequisites
Following are the minimum requirements that must be met before attempting to install Ditto.
-
Node.js active or maintenance long-term support (LTS) version
-
Linux, macOS version 11+, or Windows version 10+
Installing Dependencies
Install the @dittolive/ditto
package into your project:
Integrating 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 by replacing the placeholders in the snippet below with values obtained from the Ditto portal:
-
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
instance 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 Getting Playground Token Credentials . For an introduction to authentication in Ditto, see Security > Cloud Authentication.
Running Ditto
You can now run your basic snippet using Node and should not see any errors:
Add a Sync Subscription
Add a sync subscription to receive updates on documents that match its query from connected peers in the mesh.
Use registerSubscription()
in the ditto.sync
namespace to add a sync subscription:
Listening to Changes
Now that your app is ready to receive updates to documents from other peers, you might want to react to those changes as soon as they happen. For this you can use the registerObserver()
method on ditto.store
. The passed in callback closure is invoked by Ditto everytime it registers new changes to documents matching the query.
Was this page helpful?