realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.62k stars 558 forks source link

Realm for React Native not work offline #6554

Closed mancioshell closed 1 month ago

mancioshell commented 2 months ago

How frequently does the bug occur?

Always

Description

Hi, i’m using realm for a react native application. I receive SyncError: Failed to connect to sync: Host not found (authoritative) error when turn off wifi. But i have read mongo realm is offline first…

I open realm like this:

function AuthProvider() {
  const [visible, setVisible] = useState<boolean>(false);
  const [error, setError] = useState<string>('');

  const user = useUser();
  console.log('user', user);

  const realmAccessBehavior: Realm.OpenRealmBehaviorConfiguration = {
    type: Realm.OpenRealmBehaviorType.DownloadBeforeOpen,
    timeOutBehavior: Realm.OpenRealmTimeOutBehavior.OpenLocalRealm,
    timeOut: 3000,
  };

  return (
    <RealmProvider
      schema={[
        Invoice,
        Operator,
        Customer,
        Report,
        ReportDetail,
        Manpower,
        Material,
        RightToCall,
      ]}
      sync={{
        flexible: true,
        newRealmFileBehavior: realmAccessBehavior,
        existingRealmFileBehavior: realmAccessBehavior,
        initialSubscriptions: {
          update(subs, realm) {
            subs.add(realm.objects(Invoice));
            subs.add(realm.objects(Customer));
            subs.add(realm.objects(Operator));
            subs.add(realm.objects(Report));
          },
          rerunOnOpen: true,
        },
        onError: (_session, err) => {
          console.log('error', err);
          setError(err.message);
          setVisible(true);
        },
      }}>
      <App />
      <ErrorDialog
        errorTitle="Errore di Sincronizzazione"
        errorMsg={error}
        visible={visible}
        setVisible={setVisible}
      />
    </RealmProvider>
  );
}

Stacktrace & log output

03-14 16:25:28.255 6242 6302 W ReactNativeJS: 03-14 16:25:28.255 6242 6302 W ReactNativeJS: URL: 10.0.2.2:8081 03-14 16:25:28.255 6242 6302 W ReactNativeJS: 03-14 16:25:28.255 6242 6302 W ReactNativeJS: Error: Software caused connection abort 03-14 16:25:28.389 6242 6302 I ReactNativeJS: 'Connection state changed', 'active' 03-14 16:25:29.083 6242 6302 I ReactNativeJS: 'Connection state changed', 'active' 03-14 16:25:29.099 6242 6302 E ReactNativeJS: [error] Failed to resolve 'ws.eu-west-1.aws.realm.mongodb.com:443': Host not found (authoritative) 03-14 16:25:29.100 6242 6302 I ReactNativeJS: 'Connection state changed', 'active' 03-14 16:25:29.101 6242 6302 I ReactNativeJS: 'error', { [SyncError: Failed to connect to sync: Host not found (authoritative)] 03-14 16:25:29.101 6242 6302 I ReactNativeJS: name: 'SyncError', 03-14 16:25:29.101 6242 6302 I ReactNativeJS: isOk: false, 03-14 16:25:29.101 6242 6302 I ReactNativeJS: reason: 'Failed to connect to sync: Host not found (authoritative)', 03-14 16:25:29.101 6242 6302 I ReactNativeJS: code: 1034, 03-14 16:25:29.101 6242 6302 I ReactNativeJS: category: 'UNKNOWN', 03-14 16:25:29.101 6242 6302 I ReactNativeJS: logUrl: '', 03-14 16:25:29.101 6242 6302 I ReactNativeJS: userInfo: {}, 03-14 16:25:29.101 6242 6302 I ReactNativeJS: isFatal: false } 03-14 16:25:29.104 6242 6302 I ReactNativeJS: 'user', { listeners: 03-14 16:25:29.104 6242 6302 I ReactNativeJS: { listeners: {}, 03-14 16:25:29.104 6242 6302 I ReactNativeJS: options: { add: [Function: add], remove: [Function: remove] } }, 03-14 16:25:29.104 6242 6302 I ReactNativeJS: internal: {}, 03-14 16:25:29.104 6242 6302 I ReactNativeJS: app: 03-14 16:25:29.104 6242 6302 I ReactNativeJS: { listeners: 03-14 16:25:29.104 6242 6302 I ReactNativeJS: { listeners: {}, 03-14 16:25:29.104 6242 6302 I ReactNativeJS: options: { add: [Function: add], remove: [Function: remove] } }, 03-14 16:25:29.104 6242 6302 I ReactNativeJS: internal: {} }, 03-14 16:25:29.104 6242 6302 I ReactNativeJS: cachedProfile: undefined } 03-14 16:25:29.323 6242 6302 I ReactNativeJS: 'user', { listeners: 03-14 16:25:29.323 6242 6302 I ReactNativeJS: { listeners: {}, 03-14 16:25:29.323 6242 6302 I ReactNativeJS: options: { add: [Function: add], remove: [Function: remove] } }, 03-14 16:25:29.323 6242 6302 I ReactNativeJS: internal: {}, 03-14 16:25:29.323 6242 6302 I ReactNativeJS: app: 03-14 16:25:29.323 6242 6302 I ReactNativeJS: { listeners: 03-14 16:25:29.323 6242 6302 I ReactNativeJS: { listeners: {}, 03-14 16:25:29.323 6242 6302 I ReactNativeJS: options: { add: [Function: add], remove: [Function: remove] } }, 03-14 16:25:29.323 6242 6302 I ReactNativeJS: internal: {} }, 03-14 16:25:29.323 6242 6302 I ReactNativeJS: cachedProfile: undefined }

Can you reproduce the bug?

Always

Reproduction Steps

Version

0.6.2

What services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

Windows 10, React Native, Android 14

Build environment

adb logcat

Cocoapods version

No response

sync-by-unito[bot] commented 2 months ago

➤ PM Bot commented:

Jira ticket: RJS-2759

kneth commented 2 months ago

Host not found (authoritative) means you are offline. It is a bit confusing that the sync client reports it as an error.

From the title of the issue, it sounds like you are experiencing a variation of https://github.com/realm/realm-core/issues/7349. We are actively working on a fix.

mancioshell commented 1 month ago

@kneth is it possible to do a workaround in the meanwhile you have done the fix? i need to work in offline mode, but unfortunately the app crash when i receive the error.

mancioshell commented 1 month ago

For anyone has my same problem, i have downgraded the peerDependency realm to 11.10.2 and it works like a charm.

riohifi commented 1 month ago

const behaviorConfiguration = { type: OpenRealmBehaviorType.OpenImmediately, timeOut: 3000, timeOutBehavior: OpenRealmTimeOutBehavior.OpenLocalRealm, };

OpenImmediately not DownloadBeforeOpen then works

mancioshell commented 1 month ago

@riohifi Not for me. I have already try this configuration without success. I have solved only downgrading version.

kneth commented 1 month ago

The fix is in [v12.7.0-rc.0][https://github.com/realm/realm-js/releases/tag/v12.7.0-rc.0)

mancioshell commented 1 month ago

@kneth i have update to 12.7.9-rc.0 but now if i open the app when offline, i receive this error which cause the crash of the app:

04-04 17:43:24.278 32030 32096 E ReactNativeJS: [Realm.App - error] App: request location failed (CustomError -1): request to https://realm.mongodb.com/api/client/v2.0/app/easy-report-gmzgp/location failed, reason: Network request failed

mancioshell commented 1 month ago

please reopen the issue

vatsal-gadhiya-searce commented 1 month ago

I am getting the same issues in react native 0.73.1 app, running 12.5.0.

SyncError: Failed to connect to sync: Host not found (authoritative) [error] Failed to resolve 'ws.eu-west-2.aws.realm.mongodb.com:443': Host not found (authoritative)

Is it resolved anywhere?

Christiangsn commented 1 month ago

Is it resolved anywhere??

vatsal-gadhiya-searce commented 1 month ago

@Christiangsn I mean I am not sure if it is breaking anything or not in our app. But it comes through a bugsnag logs in our react native application using realm.

Christiangsn commented 1 month ago

@Christiangsn I mean I am not sure if it is breaking anything or not in our app. But it comes through a bugsnag logs in our react native application using realm.

Should I just ignore the error log? Because I noticed that when the internet comes back, it reconnects automatically... Another point, how can I synchronize the data? Do you have an example?