Detect realm sync device connect/disconnect

I am using MongoDB Realm for an industrial IoT project. Each device in our network authenticates with its own realm API user and syncs event logs and sensor data to Atlas.

I need to be able to notify human users if the device disconnects from the internet. What is the best way to do that? I could realm.write a ping timestamp every minute or so on the device. And have a cloud function run every minute to check the age of that ping… but I’m wondering if there is a way that doesn’t require extra network requests? Can I set up a server-side trigger that gets run on realm user connection/disconnection events?

Thanks

4 Likes

@Shea_Dawson The way you described; sending a ping across sync every so often, is how we would recommend detecting online/offline from the server-side. In the future, we may look to add a “Presence” feature which you could hook into on the backend

YES!! This is something that is really needed to expand Realm’s functionality

I put a feature request in a while back (like 2017) and then again in 2020 for the Realm Object Server (before it became MongoDB Realm)

You should add this question as a feature request for MongoDB Realm.

Hey folks, if you are using a synced Realm, you are able to detect connected status (at least in the Swift SDK)–

let token = realm.session?.observe(\SyncSession.connectionState, options: .initial) { s, _ in
    if s.connectionState == .connected {
        // do stuff
    } else if s.connectionState == .disconnected {
        // do stuff
    }
}
1 Like

Whoaaa… Great news!

Is that an actual event that will fire upon connect/disconnect or a one shot deal where it has to be called upon use?

Yes, it will fire upon connect/disconnect.

It should be noted that the API Jason referred to is for local connection/disconnection behavior, in other words, the developer can tell the user whether they are online/offline.

For a “Presence” feature on the server-side which will tell you when a device is connected or offline we’d like to add this capability as we get the request from time to time, but I’d like to clarify that this feature is likely to just be a wrapper around the design pattern I described above. That’s because the only fool proof way to determine whether a network connection is active or not - IS to send a payload across the wire and get an acknowledgement back. There are so many reasons that a socket could appear active but, for example, has yet to timeout, or has massive packet loss, or the server has failed to garbage collect the stale connection, etc. Of course there are certain optimizations that only the product can do under the hood that wouldn’t be available to the user, and we will make those - but you should not think that leveraging this feature will save you from network traffic because it is likely to use the network to determine “presence”

-Ian

1 Like

The Sync Logs now have messages “Sync → Connection Start” and “Sync → Connection End”. This means that the detection of user’s connection state changes has already been implemented. It would be great to provide the ability to create a Trigger that fires on these events - this is an essential functionality for multi-user applications, and now this is our main problem when using Realm Sync in our application. Thanks.

2 Likes