SubscribeForNotifications won't fire

Hello,

I’m building an app that listens to changes from my Realm App (on cloud), by following the procedure at this link: https://docs.mongodb.com/realm/sdk/dotnet/examples/react-to-changes

I setup some code on my PC 1 like this:

     var token = realm.All<Person>()
                    .SubscribeForNotifications((sender, changes, error) =>
    {
        Console.WriteLine("Something changed!");
    }

     while (true)
    {
        AsyncContext.Run(async () =>
        {
            realm.Refresh();
        });

        await Task.Delay(2000);
    }

On my PC 2, my code simply changed a property of an object:

realm.Write(() =>
{
    person.FullName = person.FirstName + " " + person.LastName;
});

I expected that, after the change from PC 2 was committed to Realm Server, I should be able to detect the data change on PC 1.

But it seems not work. Did I miss any thing or am I using SubscribeForNotifications the wrong way?

BTW, do we have such a thing like RegexNotificationHandler in Mongodb Realm?

Thank you very much.

Hi, I’m going to make some assumptions since I don’t have a full picture of your code. Please, correct anything wrong.
Given that you’re trying to use AsyncContext I’m going to assume that the code you posted isn’t running on a thread with a synchronisation context (like a main or ui thread).
The problem with your code is that using AsyncContext.Run like you did runs realm.Refresh() on a different thread every iteration of the loop. Because a realm instance can’t be shared with multiple threads, you should get an exception.
In order to achieve what you’re trying to do, you could do something like:

AsyncContext.Run(async () =>
{
    var token = realm.All<Person>()
                    .SubscribeForNotifications((sender, changes, error) =>
    {
        Console.WriteLine("Something changed!");
    }

    while (true)
    {
        realm.Refresh();
        await Task.Delay(2000);
    }
});

In this way you are calling realm.Refresh() on the same thread were you subscribed for notifications.

As a side note when you are on a the main/UI thread, you always have a synchronisation context. This allows realm to be able to queue its own refresh without the need for the user to do so. Hence, in that case the following would just work:

var token = realm.All<Person>()
    .SubscribeForNotifications((sender, changes, error) =>
{
    Console.WriteLine("Something changed!");
}

====================================

BTW, do we have such a thing like RegexNotificationHandler in Mongodb Realm?

What do you mean by RegexNotificationHandler? In case you mean filtering notifications, we are currently working on that.

1 Like

Hi Andrea,

In fact I didn’t get such exception when running my code. Anyway, I tried your code but still no luck.

Instead, I found something else. From my machine 2, I executed a program that updates and commits Person.Fullname periodically. At the same time in machine 1, I executed another program that pick the same Person object (using realm.Find()) and print out its Fullname, also periodically.
The result is the Fullname was not changed in machine 1.
=>That means data was not synced in real-time???

About Realms.Server.RegexNotificationHandler, it’s from Legacy Realm. I used it to listen to data changes from Realm Cloud. And what I’m trying to do in this topic is to archive the same.

For me to properly help you I’d need to take a look at the full code of the 2 examples. Could it be possible for you to share both projects with me? You could send it by email to
andrea [dot] catalini [at] mongodb [dot] com
If there is something sensitive in those examples then I can supply you with a link to securely upload the them.

Hello, I have the same problem. SubscribeForNotifications function doesn’t firing in .NET app. It worked with the Legacy Realm but it is not working now. I’m using 10.3.0 Realm version and .NET 4.8.

var token = s.realm.All<Event>().SubscribeForNotifications((sender, changes, error) =>
{
     Console.WriteLine("Something changed!");
});

Hi Pavel,

As you could see from the conversation above, in order for me to help I need to see a lot more of your code and have details on the type of application you’re working on, specifically what thread the code is executed on and if it has a synchronization context.

Hi Andrea,
I’m working on Windows Forms Application.
I have created an app in my Program.cs class:
app = App.Create("application");

I’m using two functions in order to create realm object:

> ```
> public async Task GetOrLoginUser(App app)
        {
            user = await app.LogInAsync(Credentials.EmailPassword(mail, password));
        }
public async Task OpenRealm(User user)
        {
            var partition = user.Id;
            var config = new SyncConfiguration(partition, user);
            realm = await Realm.GetInstanceAsync(config);
        }
> ```

Then I’m calling these two functions:

Task continuation = GetOrLoginUser(app);
await continuation;
continuation = OpenRealm(user);
await continuation;

And in constructor of my main form I’m using SubscribeForNotifications.

There is no synchronization context in the app.

There is always a SynchronizationContext in any .NET application, regardless of the framework that you’re using. The only difference is that each .NET framework has a different implementation of SynchronizationContext. For example Windows Forms always installs a WindowsFormsSynchronizationContext on the thread on which the first form is created. (This thread is commonly called “the UI thread”.)
If you want to have a more in-depth reading on the subject, this blogpost from Microsoft is a good place to start.

Back to your problem. As explained in our documentation, once you’ve subscribed for notifications on your realm instance, you can only get notifications if this instance of realm is regularly refreshed. The SDK makes this happen automatically on the Main thread, but it’s on the user to refresh the realm subscribed to, if such realm is open on a background thread.
So, is your realm opened on the main thread? I guess yes. Where and when are you calling GetOrLoginUser and OpenRealm? All these questions could simply be answered if you could share your project, so that I can take a look at it and see what’s going on.
You can email it to me at
andrea [dot] catalini [at] mongodb [dot] com
or if there is something sensitive (like it’s not a POC) then I can supply you with a link to securely upload it.

I hope this helps.