SDK Guides
JavaScript

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:

1

Confirm that you meet the minimum requirements. (Prerequisites)

2

Install the ditto package in your environment. (Installing Dependencies)

3

Import and initialize Ditto in your app. (Integrating Ditto)

4

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.

  • Node.js active and maintenance long-term support (LTS)
  • Linux, macOS version 11, or Windows version 10.0

Installing Dependencies

Install the @dittolive/ditto package into your project:

bash


Integrating Ditto and Starting Sync

Import and initialize Ditto, and then provide your access credentials for one-time authentication with the Big Peer:

  1. From the top-most scope of your app, import Ditto.
  2. Using async await, initialize Ditto.
  3. Provide your access credentials:
    1. Replace the appID placeholder value with the app ID that identifies your app in Ditto.
    2. 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.

JS


Running Ditto

If you're using typescript, you must create the JavaScript file by running the following command:

Bash


Then, you can execute the file:

Bash


Add a Sync Subscriptions

Use the ditto.sync namespace to add a sync subscription.

JS


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.

JS