Can realm sync rules and non-sync rules be used simultaneously?

I’m thinking of building both an iOS app and a Web app that interacts with our Realm app, and I’m trying to determine my method(s) to communicate with the Realm app. I’m thinking Realm Sync could be useful for the iOS app, but I’m concerned that this will mean I won’t be able to take advantage of non-sync rules for my Web app after seeing the following message on your Define Sync Rules page:

This page describes data access rules for synced clusters. Non-synced cluster use a different rules model that sync rules override. If sync is enabled for a cluster, any non-sync rules defined for the cluster do not apply.
If your app does not use sync, check out MongoDB Collection Rules for more information on rules for non-synced clusters.

Is it possible to use the Sync Rules when communicating between the iOS app and the Realm app, while still being able to use the regular non-Sync rules for communications with the Web app? If not, then how would I communicate between the Web app and Realm app in a way that keeps users’ access to data secure if I’m using Realm Sync for the iOS app? Will the GraphQL API, direct CRUD operations, and/or Functions work? Or would I need a separate Realm app for the Web app and iOS app?

Actually, I’m not sure two Realm apps would work since user login credentials couldn’t be shared across the Realm apps, right?

Hey Elias!

You’re correct that you can’t use non-sync rules on a linked cluster while it is synced. All requests on a synced cluster go through the read and write rules you create for sync instead of the collection-level rules you define on non-synced clusters. This includes things like GraphQL, MQL queries, and functions - they will still work, just with a different rules paradigm.

If you really want to use both sync and non-sync rules in the same app, you can use a workaround though there are still downsides. Unfortunately there’s no clean solution that supports both sets of rules simultaneously.

  1. Link the cluster to your app again under a different name. Now you’ll have the same cluster twice - one synced and the other not.

  2. Define schemas and make sure to always copy changes to one cluster over to the other

  3. Define sync rules for your sync clients and non-sync rules for your other clients. Take care to make sure one set covers all the cases of the other or else you’ll have a security hole. (May as well just use the sync rules only given the maintenance and risk here)

Sync requests will use sync rules and you can direct other writes (from web apps, functions, etc) to the non-synced cluster.

1 Like

Thanks for the prompt and thorough response! I think that clears everything up for me.

Looking into Realm Sync more, I’ve found that the partitions model for setting up rules doesn’t suit my use case very well, so I think for now I will go ahead and use non-sync rules for both my iOS app and Web app.

However, in doing my research I learned that you’re currently working on an improved version of Realm Sync called Realm Flexible Sync, which sounds like it takes the best of both worlds from Realm Sync and Realm non-sync. So once that comes out, I might go ahead and transition to that model instead!