DEV Community

Cover image for Database for syncing cross platform
ObjectBox.io for ObjectBox

Posted on • Edited on

Database for syncing cross platform

How Data Sync works - quick video demo

cross platform sync c++ example

ObjectBox Database + Data Sync is a Mobile and IoT solution for fast and effortless data access using Edge Computing. Our easy native APIs (Java, Kotlin, Flutter / Dart, C / C++, Swift, Golang) are built for cross platform development. The ObjectBox database can be set up in minutes, and is also free and easy to use.

This tutorial will show you how to set up ObjectBox Sync in C++, Go and Java, and then sync data between three instances of an example app. The app we are going to use is for managing a to-do list, its functionality is very simple. User can add tasks to the list and mark them as done, while ObjectBox takes care of syncing this data across platforms.

Note: if you haven't used ObjectBox DB yet, here are the docs for each language binding: Java/Kotlin, C/C++, Flutter/Dart, Go, Swift.

A couple of notes about the example

  • To follow this tutorial, you need to get your own copy of the Sync Server (available as an executable for Linux and as a Docker container). You can apply for a free Sync Trial here. You will only need to start the server once, but we included a step about it in for all three examples. Skip those if you already have a server running.

  • As this is just an example, we opted for no authentication to make things simple. This is not what you would use in production. We currently offer two authentication methods: shared secret and Google Sign-In. Here is the section about authentication options in the Sync docs.

  • If building your own solution, you will have to make sure that the UIDs in the model JSON file are the same across all of your apps. More details about UIDs in the Sync docs.

How to run the example apps

C++ example app

Example code repository

Video tutorial

cross platform sync c++ example

Requirements

New to C++? Here is our tutorial covering the installation basics.

  • WSL Ubuntu
  • CMake
  • Git
  • C++
  • Clang

Step-by-step

  1. Create CMakelists.txt:
include(FetchContent) 
FetchContent_Declare( 
objectbox 
GIT_REPOSITORY https://github.com/objectbox/objectbox-c.git 
GIT_TAG v0.15.2 
) 

FetchContent_MakeAvailable(objectbox) 

add_executable(myapp main.cpp) 
target_link_libraries(myapp objectbox-sync)
Enter fullscreen mode Exit fullscreen mode
  1. Configure and build the project using CMake: Configure (Clang), and then CMake: Build.

  2. [if not running a server already] Start the ObjectBox Sync Server on Linux by running ./sync-server --model build/_deps/objectbox-src/examples/cpp-gen/objectbox-model.json --unsecured-no-authentication, where sync-server is the path to your sync server executable.

  3. Now launch objectbox-c-examples-cpp-gen-sync, and the Sync Client will start automatically. You can see how it was implemented in main.cpp.

  4. Add a new task (e.g. new task-cpp-1), and open the Admin UI to check if the tasks appears there. To do this, just type http://127.0.0.1:9980/ in any web browser.


Go example app

Example code repository

Video tutorial

cross platform sync c++ example

Requirements

  • Go (see how to configure it for VS Code here)

Step-by-step

  1. Clone the objectbox-go repository to your VS Code project. Make sure the current directory is objectbox-go.

  2. This app is actually two examples in one: with sync and without. To run the one with sync, we need to enable the Task object for syncing: simply put // objectbox:"sync" on a new line in examples/tasks/internal/model/task.go.

  3. Now we need to update the ObjectBox schema by running the generator: go generate examples/tasks/internal/model/task.go.

  4. [if not running a server already] Now start the ObjectBox Sync Server: ./sync-server --model=examples/tasks/internal/model/objectbox-model.json --unsecured-no-authentication, where sync-server is the path to your sync server file.

  5. Run go run examples/tasks/main.go.

  6. Add a task (e.g. new task-go) and verify that it synced correctly with the server. Simply open http://127.0.0.1:9980/ in a web browser.


Java (Andorid) example app

Example code repository

Requirements

  • Java
  • Android Studio

Step-by-step

  1. Open Android Studio and go to File → New → Project from Version Control to clone the objectbox-examples repository. Use this URL: https://github.com/objectbox/objectbox-examples.git

  2. [if not running a server already] Start the ObjectBox Sync Server:
    ./sync-server --model android-app-sync/objectbox-models/default.json --unsecured-no-authentication, where sync-server is the path to your sync server file.

  3. Run “android-app-sync” on a device of your choice.

  4. Add a new task called, e.g. “task-java”. Open the Admin UI to check if the task appears there: http://127.0.0.1:9980/.

Next Steps

Now it's your turn. Build your own cross platform app using any combination of the supported languages. Don't forget to go the Sync docs when doing so. They cover all aspects that we've presented today in more detail.

We'd love to see what you've built with ObjectBox DB + Sync! Share your apps with us on Social Media by tagging @objectbox_io, or just send us an email on contact[at]objectbox.io.

Top comments (0)