Problem with setting up Realm Sync of Local and Cloud databases in .Net Project

I would be glad, if someone could help out, because I have been suffering with this problem for the days, the problem is with setting up Realm Sync of Local and Cloud databases in .Net(C#) Project.

I can connect to Cloud and write and retrieve the data, but I can’t figure out, how to have the local Realm on my computer sync with Cloud(in other words, when offline → not connected to Realm, see the updates on Realm Studio). To make the issue more clear, let’s say I don’t connect to Realm Cloud and update the local Realm database, I want to see this update immediately on Realm Object Server.

SyncConfigurationBase.Initialize(UserPersistenceMode.NotEncrypted);

var authUrl = new Uri("https://MY-REALM-CLOUD-NAME.cloud.realm.io");
var credentials = Credentials.UsernamePassword("MY-USERNAME", "MY-PASSWORD", createUser: false);
var user = await User.LoginAsync(credentials, authUrl);

var realmUrl = new Uri("realms://MY-REALM-CLOUD-NAME.cloud.realm.io/~/myRealm");

var configuration = new FullSyncConfiguration(realmUrl,user: user);


//Realm realm = Realm.GetInstance(new RealmConfiguration(Environment.CurrentDirectory+"/database.realm"));
Realm realm = await Realm.GetInstanceAsync();
realm.Write(() =>
                {
                    realm.Add(new Dog { Name = "1111", Age = 2 });
                });
                var oldDogs=realm.All<Dog>().Where(d => d.Age > 1);
                foreach (var d in oldDogs)
                {
                    Console.WriteLine(d.Name);
                }

@gamer_life This generally looks correct to me, although I’m not sure why you are return realm;
are you trying to sync the writes on your local realm to cloud? And you are saying this is not working when you come back online? Can you post some logs here? You can set the log level here:

@Ian_Ward Thank you so much for the feedback. I am sorry, return realm; was inside of function and I was making the code look compact and nice, as a result I forgot to remove that part in this post.
Q: are you trying to sync the writes on your local realm to cloud?
A: Exactly, I am trying to have sync between local realm with cloud, unfortunately I can’t fully understand how to accomplish this. I tried with subscribe function from Realm, but I can’t figure out why still it doesn’t keep my local Realm database always synced with Cloud Realm.
Q: And you are saying this is not working when you come back online?
A: With above code, all of the entries are being uploaded only to Realm Cloud and I can’t see the local Realm Database with those uploaded entries. So, the main idea is that local Realm database(let’s say endpoint) shall be always in sync with Realm Cloud.
Q: Can you post some logs here?
A: Below are the logs:

Realm sync client ([realm-core-5.23.8], [realm-sync-4.9.5])
Supported protocol versions: 26-30
Platform: Linux Linux 4.15.0-91-generic #92~16.04.1-Ubuntu SMP Fri Feb 28 14:57:22 UTC 2020 x86_64
Build mode: Release
Config param: max_open_files = 256
Config param: one_connection_per_session = 1
Config param: connect_timeout = 120000 ms
Config param: connection_linger_time = 30000 ms
Config param: ping_keepalive_period = 60000 ms
Config param: pong_keepalive_timeout = 120000 ms
Config param: fast_reconnect_limit = 60000 ms
Config param: disable_upload_compaction = 0
Config param: tcp_no_delay = 0
Config param: disable_sync_to_disk = 0
User agent string: 'RealmSync/4.9.5 (Linux Linux 4.15.0-91-generic #92~16.04.1-Ubuntu SMP Fri Feb 28 14:57:22 UTC 2020 x86_64) RealmDotNet/4.3.0.0 (.NET Core 3.1.2) ’
Connection[1]: WebSocket::Websocket()
Connection[1]: Session[1]: Binding ‘/home/cool/realm-object-server/90adb87d-8624-4bee-beba-946c0ad1f66f/realms%3A%2F%2Fptx-test.de1a.cloud.realm.io%2F%7E%2FmyRealm’ to ‘/4219523c68c25db5b31ff2b3e48face4/myRealm’
Connection[1]: Session[1]: Activating
Connection[1]: Session[1]: client_reset_config = false, Realm exists = true, async open = false, client reset = false
Opening Realm file: /home/cool/realm-object-server/90adb87d-8624-4bee-beba-946c0ad1f/realms%3A%2F%2FMY-REALM.cloud.realm.io%2F%7E%2FmyRealm
Connection[1]: Session[1]: client_file_ident = 2, client_file_ident_salt = 13912707436647382
Connection[1]: Session[1]: Progress handler called, downloaded = 57, downloadable(total) = 57, uploaded = 873, uploadable = 930, reliable_download_progress = 0, snapshot version = 33
Connection[1]: Resolving 'MY-REALM.cloud.realm.io:443'
Connection[1]: Connecting to endpoint ‘3.120.234.243:443’ (1/3)
Connection[1]: Connected to endpoint ‘3.120.234.243:443’ (from ‘192.168.0.104:414’)
Connection[1]: Verifying server SSL certificate using root certificates, host name = MY-REALM.cloud.realm.io, server port = 443, certificate =
-----BEGIN CERTIFICATE-----
MII… DUE TO CONFIDENTIAL INFO, THE REST OF CERTIFICATE WAS REMOVED
-----END CERTIFICATE-----

Connection[1]: Verifying server SSL certificate using 155 root certificates
Connection[1]: Server SSL certificate verified using root certificate(29):
-----BEGIN CERTIFICATE-----
MII… DUE TO CONFIDENTIAL INFO, THE REST OF CERTIFICATE WAS REMOVED
-----END CERTIFICATE-----

Connection[1]: WebSocket::initiate_client_handshake()
Connection[1]: WebSocket::handle_http_response_received()
Connection[1]: Negotiated protocol version: 30
Connection[1]: Will emit a ping in 43708 milliseconds
Connection[1]: Session[1]: Sending: BIND(path=‘/4219523c68c25db5b31ff2b3e48face4/myRealm’, signed_user_token_size=786, need_client_file_ident=0, is_subserver=0)
Connection[1]: Session[1]: Sending: IDENT(client_file_ident=2, client_file_ident_salt=1391270743664738180, scan_server_version=15, scan_client_version=30, latest_server_version=15, latest_server_version_salt=7570085601498466528)
Connection[1]: Session[1]: Sending: MARK(request_ident=2)
Connection[1]: Session[1]: Received: DOWNLOAD(download_server_version=16, download_client_version=33, latest_server_version=16, latest_server_version_salt=7570085601498466528, upload_client_version=33, upload_server_version=15, downloadable_bytes=0, num_changesets=0, …)
Connection[1]: Session[1]: Progress handler called, downloaded = 57, downloadable(total) = 57, uploaded = 930, uploadable = 930, reliable_download_progress = 1, snapshot version = 34
Connection[1]: Session[1]: Received: MARK(request_ident=2)
Connection[1]: Session[1]: Sending: UPLOAD(progress_client_version=34, progress_server_version=16, locked_server_version=16, num_changesets=0)
ooooooooo
ooooooooo2
ooooooooo2
ooooooooo2
ooooooooo2
ooooooooo2
3333333
3333333
3333333
1111
22220000000000
1111
1111
1111
1111
1111
Connection[1]: Session[1]: Sending: UPLOAD(progress_client_version=35, progress_server_version=16, locked_server_version=16, num_changesets=1)
Connection[1]: Session[1]: Upload compaction: original size = 57, compacted size = 57
Connection[1]: Session[1]: Progress handler called, downloaded = 57, downloadable(total) = 57, uploaded = 930, uploadable = 987, reliable_download_progress = 1, snapshot version = 35

From the logs I have seen that there is realm-object-server Folder on my computer and it’s there. I assume this is local version of Realm Cloud, but I wonder, how can I pass the file path to Realm sync configuration, so that Realm Cloud folder for certain Project is located inside of Project directory.

You do not need to pass in the file path. This file is created automatically for you when you call getInstanceAsync. When you make a write to a realm that is opened with a sync configuration you do not need to explicitly call sync - it does this automatically for you in the background. Just keep the realm reference alive and do not let it garbage collect and it will continue syncing.

I think, I wasn’t fully clear with the issue. Let’s consider this consequence, I update the local Realm Cloud database without initiating connection(not connected to Realm Cloud, because there was no internet connection available) and after the connection to Realm Cloud with User.LoginAsync(credentials, authUrl) , I want to see these changes that were made while offline(not connected to Realm Cloud, because there was no internet connection available) synced with Realm Cloud. The problem, I can’t figure out how to access the local Realm Cloud database in code in order to update it while offline and I want to have these offline changes being synced with Realm Cloud once the connection with Realm Cloud is established.

@gamer_life OK - I understand now. You only need to logOn (and be online) once to validate crednetials, the first time the user logs in. After that, the credentials are cached locally and the synced realm can be opened even when offline by making a call to currentUser as described here:

So the steps would be

  • Log In
  • Open and Download the realm
  • Go Offline
  • Use currentUser to open the synced Realm that was opened previously
  • Make changes/writes
  • Go back online
  • Realm syncs to server side
1 Like

@Ian_Ward Thanks a lot for the answer. I have done everything as you have mentioned above, but still for some unknown reason it doesn’t sync. Could you please have a look and propose or update the code to resolve this issue. Below is code, which I am using:

            User user=User.Current;
            //var serverURL = new Uri("/~/myRealm", UriKind.Relative);
            var serverURL = new Uri("/default", UriKind.Relative);
            
            var configuration = new FullSyncConfiguration(serverURL, user);

            var realm = Realm.GetInstance(configuration);

            realm.Write(() =>
                {
                    realm.Add(new Dog { Name = "......", Age = 2 });
                });
            var oldDogs=realm.All<Dog>().Where(d => d.Age > 1);
                foreach (var d in oldDogs)
                {
                    Console.WriteLine(d.Name);
                }

Console logs are below:

Realm sync client ([realm-core-5.23.8], [realm-sync-4.9.5])
Supported protocol versions: 26-30
Platform: Linux Linux 4.15.0-91-generic #92~16.04.1-Ubuntu SMP Fri Feb 28 14:57:22 UTC 2020 x86_64
Build mode: Release
Config param: max_open_files = 256
Config param: one_connection_per_session = 1
Config param: connect_timeout = 120000 ms
Config param: connection_linger_time = 30000 ms
Config param: ping_keepalive_period = 60000 ms
Config param: pong_keepalive_timeout = 120000 ms
Config param: fast_reconnect_limit = 60000 ms
Config param: disable_upload_compaction = 0
Config param: tcp_no_delay = 0
Config param: disable_sync_to_disk = 0
User agent string: 'RealmSync/4.9.5 (Linux Linux 4.15.0-91-generic #92~16.04.1-Ubuntu SMP Fri Feb 28 14:57:22 UTC 2020 x86_64) RealmDotNet/4.3.0.0 (.NET Core 3.1.2) ’

The code looks fine to me and there is nothing in the logs to indicate what is going wrong. Sorry I can’t be of more help - the .NET tutorial should work for this except for updating the APIs where Visual Studio tells you to