C++

First follow the instructions to compile and install LiteSync from the source code or use the pre-compiled binaries for your platform. You can start with the free version.


Example code
#include <sqlite_modern_cpp.h>
#include <thread>
#include <chrono>
#include <iostream>

using namespace sqlite;

int main() {
  try {
    // open the database
    database db("file:app.db?node=secondary&connect=tcp://server:port");

    // wait until the database is ready
    while(1) {
      string status;
      db << "pragma sync_status" >> status;
      std::cout << "status : " << status << std::endl;
      if (status.find("\"db_is_ready\": true") != string::npos) break;
      std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    // now we can use the db connection
    ...
  }
  catch (std::exception& e) {
    std::cerr << e.what() << std::endl;
  }
}


Update Notification

Your application can be notified when the local database is updated due to synchronization with remote nodes. The notification is done via a user-defined function.

db.define("update_notification", [](std::string changes) {
  std::cout << "update received: " << changes << std::endl;
});


Transaction Notification

Your application can be notified when local transactions are synchronized with remote nodes. The notification is done via a user-defined function. If the value in the `result` argument is not "OK" then it contains the error message.

db.define("transaction_notification", [](std::string sql, std::string result) {
  std::cout << "Transaction synchronized (" << result << "): " << sql << std::endl;
});


Synchronization Status

Your application can check the synchronization status of the local database with the remote nodes.

std::string status;
db << "pragma sync_status" >> status;
std::cout << "sync status: " << status << std::endl;


ATTENTION: The notification functions are called by the worker thread. The application should NOT use the db connection inside the notification functions and it must return as fast as possible! The application can transfer the notification to the main thread before returning.




Build

g++ -o app app.cpp -std=c++14 -llitesync