Skip to main content

Class: Sync

The Sync Class allows you to create Rich and Dynamic Experiences in your websites and applications using Live Data from Twilio Sync

Throughout this page, you will see syncClient with a lowercase "s" and Sync with an uppercase "S".

Sync refers to the Sync object provided by the SDK.

syncClient represents an instance of a Sync, created with the new keyword:

Instantiate a Sync Client​

A new instance of Sync should be constructed by using the new keyword and passing the Config payload as the first argument.

No signaling channel will be opened when instantiating a new Sync Client. The signaling WebSocket will be opened when a syncClient.registerDocument() syncClient.registerMap() is called.

The maximum number of characters for the identity provided in the Access Token is 121. The identity may only contain alpha-numeric and underscore characters. Other characters, including spaces, or exceeding the maximum number of characters, will result in not being able to place or receive calls.

If a KioskId is provided when the syncClient is Created then the Kiosk Document for that KioskId is automatically Registered.

Initiate a Basic Sync Client​

const syncClient = new Sync({
accountSid: "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
identity: "FIXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
});

To see all of the possible properties, see the Config interface section.

Hierarchy​

  • EventEmitter

    ↳ Sync

Accessors​

client​

• get client(): Client

Returns the underlaying Twilio SDK Client for advanced configuration and usage

    console.info(syncClient.client.connectionState);

Returns​

Client


isSupported​

• get isSupported(): boolean

Returns if Sync is Supported in the current Browser / Context

    console.info(syncClient.isSupported)

Returns​

boolean


ready​

• get ready(): boolean

Is the syncClient ready?

    syncClient.ready ? console.info("Ready") ? console.info("Not Ready")

Returns​

boolean

Methods​

disconnect​

▸ disconnect(): Promise<void>

Disconnect Sync Client

  syncClient.disconnect();

Returns​

Promise<void>


document​

▸ document(name): SyncDocument

Returns the Specified "Document"

    console.info(syncClient.document["myDocument"].lastEventId)

Parameters​

NameType
namestring

Returns​

SyncDocument


init​

▸ init(): Promise<void>

Init the Sync Client

Should only be used if the Client is destroyed and a "re-init" is required for some bizare reason

    await syncClient.init();

Returns​

Promise<void>


map​

▸ map(name): SyncMap

Returns the Specified "Map"

    console.info(syncClient.map["myMap"].lastEventId)

Parameters​

NameType
namestring

Returns​

SyncMap


registerDocument​

▸ registerDocument(Payload): Promise<SyncDocument>

Register a Sync Document

  const document = syncClient.registerDocument({ name: "myDocument" });

Parameters​

NameType
PayloadRegisterDocumentPayload

Returns​

Promise<SyncDocument>


registerMap​

▸ registerMap(Payload): Promise<SyncMap>

Register a Sync Map

  const map = syncClient.registerMap({ name: "myMap" });

Parameters​

NameType
PayloadRegisterMapPayload

Returns​

Promise<SyncMap>


updateToken​

▸ updateToken(newToken): void

Manually update Race Authentication Token

    syncClient.on("sync#tokenAboutToExpire", () => {
// Get New Token
syncClient.updateToken(newToken);
});

Parameters​

NameType
newTokenstring

Returns​

void