db.syncable.connect()

Syntax

db.syncable.connect (
  protocol: string,
  url: string,
  options: Object
) : Promise<void>

Parameters

protocol : String Name of protocol. An Implementation of ISyncProtocol must have been registered using Dexie.Syncable.registerSyncProtocol()
url : StringURL to connect to
options : ObjectOptions that the registered protocol can handle. Options are specific to each ISyncProtocol implementation

Return Value

Promise

Description

Connect to given URL using given protocol and options. Returned Promise will resolve when sync-protocol has called its onSuccess() callback. If sync-protocol calls onError(), the returned promise will reject but the underlying framework may continue to try connecting in the background. If you want to stop this, call db.syncable.disconnect (url) in your catch clause.

Once connected, you will never have to call connect() again. Not even after a reboot. The framework will keep in sync forever with this node until a call to disconnect(), delete() happens, or if an irrepairable error occur on the node (not network down, but a fatal error).

The very first time that connect() is called upon an URL, the framework will start syncing the entire indexedDB database towards the sync protocol implementation. If this is not desired, supply option {initialUpload: false} in your connect call.

Calling connect() when the node is already connected, will not generate any sync operation but result in an immediate resolve of returned Promise.

Sample

import Dexie from 'dexie';
import 'dexie-observable';
import 'dexie-syncable';

//
// 1. Register Your Protocol(s)!
//
Dexie.Syncable.registerSyncProtocol("myProtocol", {
    sync: function (...) {...}; // An ISyncProtocol implementation.
});

//
// 2. Declare Your Database.
//
var db = new Dexie('mydb');
db.version(1).stores({foo: 'id'});

//
// 3. Call db.syncable.connect().
//
db.syncable.connect(
    "myProtocol",
    "https://remote-server/...",
    {options...})
.catch(err => {
    console.error (`Failed to connect: ${err.stack || err}`);
});

//
// 4. Start using the database as normal.
//
db.foo.toArray().then(foos => {
    // This promise will not resolve before an initial sync has taken place.
    // For sure, your data will be populated from server when you get here.
    // Very First Time - you need to be online to get here!
    // After that: you can get here even if you are offline.
}).catch(err => {
    // May happen if this was you app's initial load and you were offline.
});

See also

Dexie.Syncable.js

Table of Contents