This is the API reference documentation for the Ditto C++ SDK.
#include "Ditto.h"
#include <chrono>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
constexpr const char *DITTO_APP_ID = "replace with your app ID";
constexpr const char *DITTO_AUTH_URL = "replace with your authorization URL";
constexpr const char *DITTO_WEBSOCKET_URL = "replace with your websocket URL";
constexpr const char *DITTO_ONLINE_PLAYGROUND_TOKEN =
"replace with your playground token";
constexpr const char *PERSISTENCE_DIR = "/tmp/ditto-cpp-testapp";
constexpr const char *LOG_EXPORT_PATH = "./ditto-logexport.jsonl.gz";
void export_ditto_log() noexcept {
try {
if (std::filesystem::exists(LOG_EXPORT_PATH)) {
std::filesystem::remove(LOG_EXPORT_PATH);
}
std::cerr << "info: exported log to " << LOG_EXPORT_PATH << std::endl;
} catch (const std::exception &err) {
std::cerr << "error: unable to export log dump: " << err.what()
<< std::endl;
}
}
int main(int argc, const char **argv) {
try {
std::cerr << "info: running Ditto test app with SDK version "
DITTO_APP_ID, DITTO_ONLINE_PLAYGROUND_TOKEN, false, DITTO_AUTH_URL);
auto config = ditto::Ditto::Config::default_config()
.set_identity(identity)
.set_persistence_directory(PERSISTENCE_DIR);
});
ditto->disable_sync_with_v3();
std::mutex mtx;
auto observer =
ditto->get_store().register_observer(
"SELECT * FROM tasks WHERE isDeleted = false",
const auto items = query_result.
items();
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Update for collection 'tasks':\n";
for (const auto &item : items) {
std::cout << " " << item.json_string() << "\n";
}
std::cout << "End of update for collection 'tasks'\n";
});
auto subscription =
ditto->get_sync().register_subscription(
"SELECT * FROM tasks");
{
std::lock_guard<std::mutex> lock(mtx);
std::cout << "(Waiting for 15 seconds...)" << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(15));
subscription->cancel();
observer->cancel();
export_ditto_log();
} catch (const std::exception &e) {
std::cerr << "error: " << e.what() << std::endl;
export_ditto_log();
std::exit(EXIT_FAILURE);
}
return 0;
}
std::set< std::string > websocket_urls
Definition TransportConfig.hpp:274
static std::string get_sdk_version()
Returns a string identifying the version of the Ditto SDK.
static std::shared_ptr< Ditto > open(DittoConfig config=DittoConfig::default_config())
Create a new Ditto instance.
static std::shared_ptr< OnlinePlaygroundIdentity > OnlinePlayground(std::string app_id, std::string token, bool enable_ditto_cloud_sync=true, std::string custom_auth_url="")
Construct a new OnlinePlaygroundIdentity.
static void set_minimum_log_level(LogLevel log_level)
static std::future< uint64_t > export_to_file(std::string const &file_path)
Exports collected logs to a compressed and JSON-encoded file on the local file system.
Represents the returned results when executing a DQL query, containing a QueryResultItem for each mat...
Definition QueryResult.hpp:121
std::vector< QueryResultItem > items() const
A configuration object specifying which network transports Ditto should use to sync data.
Definition TransportConfig.hpp:374
Connect connect
Definition TransportConfig.hpp:385
void enable_all_peer_to_peer()
Definition TransportConfig.hpp:400
Namespace for the Ditto C++ SDK types and functions.
Definition AbstractDocumentPath.hpp:19