Database for syncing cross platform
Published: 2022-03-25 12:10:14

## How Data Sync works - quick video demo
<a href="http://www.youtube.com/watch?feature=player_embedded&v=Feiq46u3hH0" target="_blank"><img src="http://img.youtube.com/vi/Feiq46u3hH0/0.jpg" alt="cross platform sync c++ example" width="240" height="180" border="10" /></a>

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](https://docs.objectbox.io/), [Flutter / Dart](https://pub.dev/packages/objectbox), [C / C++](https://cpp.objectbox.io/), [Swift](https://swift.objectbox.io/), [Golang](https://golang.objectbox.io/)) are built for cross platform development. [The ObjectBox database can be set up in minutes, and is also free and easy to use](https://github.com/objectbox). 

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](https://docs.objectbox.io/), [C/C++](https://cpp.objectbox.io/), [Flutter/Dart](https://docs.objectbox.io/), [Go](https://golang.objectbox.io/), [Swift](https://swift.objectbox.io/).


## 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](https://objectbox.io/sync/). 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](https://sync.objectbox.io/sync-client#authentication-options).

* 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](https://docs.objectbox.io/advanced/meta-model-ids-and-uids).


## How to run the example apps


### C++ example app

[Example code repository](https://github.com/objectbox/objectbox-c/tree/main/examples/cpp-gen-sync)

#### Video tutorial
<a href="http://www.youtube.com/watch?feature=player_embedded&v=PDSXPVHgC4Y" target="_blank"><img src="http://img.youtube.com/vi/PDSXPVHgC4Y/0.jpg" alt="cross platform sync c++ example" width="240" height="180" border="10" /></a>

#### Requirements
New to C++? [Here is our tutorial covering the installation basics](https://dev.to/objectbox/how-to-get-started-with-objectbox-database-4eo4).
* 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)
```

2. Configure and build the project using CMake: Configure (Clang), and then CMake: Build.

3. [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.

4. 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.

5. 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](https://github.com/objectbox/objectbox-go/tree/main/examples/tasks)

#### Video tutorial
<a href="http://www.youtube.com/watch?feature=player_embedded&v=kLrqRl80qvs" target="_blank"><img src="http://img.youtube.com/vi/kLrqRl80qvs/0.jpg" alt="cross platform sync c++ example" width="240" height="180" border="10" /></a>

#### Requirements
* Go (see how to configure it for VS Code [here](https://docs.microsoft.com/en-us/azure/developer/go/configure-visual-studio-code))

#### 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](https://github.com/objectbox/objectbox-examples/tree/main/android-app-sync)
#### 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](https://sync.objectbox.io/) 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.