cometchat / cometchat-sample-app-react-native

Voice & Video Calling and Text Chat App for React Native
https://www.cometchat.com
Other
332 stars 166 forks source link

Cannot read property shouldAutoEstablishSocketConnection #91

Closed soldatov-ss closed 7 months ago

soldatov-ss commented 7 months ago

Cannot read property shouldAutoEstablishSocketConnection of undefined, js engine: hermes It's ok when I just run the application, but when I build it to apk file for android and then try to open the app it does not work.

related stack overflow issue

Снимок экрана от 2024-03-18 11-54-51

Environment

"react-native": "0.72.8", "@cometchat/chat-sdk-react-native": "^4.0.5",

App.tsx:

import React, { useCallback, useEffect } from 'react';
import { getTrackingStatus, requestTrackingPermission } from 'react-native-tracking-transparency';

import i18next from '@/services/i18next';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { I18nextProvider } from 'react-i18next';

import { Provider } from 'react-redux';
import { persistor, store } from '@/redux';
import { PersistGate } from 'redux-persist/integration/react';
import { AppNavigation } from '@/navigation';
import { GoogleSignin } from '@react-native-google-signin/google-signin';
import { BottomSheetModalProvider } from '@gorhom/bottom-sheet';
// eslint-disable-next-line no-duplicate-imports
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { NotificationToast } from '@/components/NotificationToast/NotificationToast';
import { Alert, Linking, StatusBar, useColorScheme } from 'react-native';
import { save, get } from './src/utils/asyncStorage/asyncStorage';
import appleAuth from '@invertase/react-native-apple-authentication';
import { isAndroid } from '@/utils/platform/isAndroid';
import { CometChat } from '@cometchat/chat-sdk-react-native';

function App(): JSX.Element {
  const appearance = useColorScheme();

  const initCometChat = () => {
    const appID = process.env.COMET_CHAT_APP_ID;
    const region = process.env.COMET_CHAT_REGION;

    const appSetting = new CometChat.AppSettingsBuilder()
      .subscribePresenceForAllUsers()
      .setRegion(region)
      .autoEstablishSocketConnection(true)
      .overrideAdminHost(`${appID}.api-${region}.cc-cluster-2.io/v3`)
      .overrideClientHost(`${appID}.apiclient-${region}.cc-cluster-2.io/v3`)
      .build();
    CometChat.init(appID, appSetting).then(
      () => {
        console.log('Comet Chat initialization completed successfully');
      },
      error => {
        console.log('Comet Chat initialization failed with error:', error);
      },
    );
  };

  const setAppTheme = useCallback(async () => {
    const IS_FIRST = await get('IS_FIRST');

    if (IS_FIRST === null) {
      save('THEME', appearance);
      save('IS_FIRST', true);
    }
  }, []);

  useEffect(() => {
    setAppTheme();
  }, [setAppTheme]);

  const tracking = async () => {
    const permissionStatus = await getTrackingStatus()

    if (permissionStatus === "not-determined") {
      const trackingStatus = await requestTrackingPermission();
      console.log("trackingStatus", trackingStatus);
      return
    }

    if (permissionStatus === "denied") {
      Alert.alert('Allow to track you activity across others companies app and websites?', ' Settings > Privacy & Security > Tracking.', [
        {
          text: 'Go to settings',
          onPress: () => Linking.openSettings(),
          style: 'cancel',
        },
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
        },
      ]);
    }

  };

  useEffect(() => {
    GoogleSignin.configure({
      iosClientId: process.env.GOOGLE_OAUTH_CLIENT_ID_IOS,
      webClientId: process.env.GOOGLE_OAUTH_CLIENT_ID_WEB,
    });
    tracking();
    initCometChat();
  }, []);

  useEffect(() => {
    if (!isAndroid()) {
      return appleAuth.onCredentialRevoked(async () => {
        console.warn(
          'If this function executes, User Credentials have been Revoked',
        );
      });
    }
  }, []);

  return (
    <Provider store={store}>
      <PersistGate persistor={persistor} loading={null}>
        <I18nextProvider i18n={i18next}>
          <GestureHandlerRootView style={{ flex: 1 }}>
            <BottomSheetModalProvider>
              <SafeAreaProvider>
                <StatusBar
                  barStyle={
                    appearance === 'light' ? 'light-content' : 'dark-content'
                  }
                />
                <NotificationToast />
                <AppNavigation />
              </SafeAreaProvider>
            </BottomSheetModalProvider>
          </GestureHandlerRootView>
        </I18nextProvider>
      </PersistGate>
    </Provider>
  );
}

export default App;
cometchat-helpcenter-bot commented 7 months ago

Kritika (CometChat Team) replied:

Hello,

Thank you for sharing the query. Happy to help.

Can you please elaborate on your query and elaborate what you are trying to achieve so we can give you an appropriate solution for the same.

Thanks & Regards Kritika Rastogi CometChat

soldatov-ss commented 7 months ago

Kritika (CometChat Team) replied:

Hello,

Thank you for sharing the query. Happy to help.

Can you please elaborate on your query and elaborate what you are trying to achieve so we can give you an appropriate solution for the same.

Thanks & Regards Kritika Rastogi CometChat

the problem is in this part code:

    const appSetting = new CometChat.AppSettingsBuilder()
      .subscribePresenceForAllUsers()
      .setRegion(region)
      .autoEstablishSocketConnection(true)
      .overrideAdminHost(`${appID}.api-${region}.cc-cluster-2.io/v3`)
      .overrideClientHost(`${appID}.apiclient-${region}.cc-cluster-2.io/v3`)
      .build();

Because if I comment out init func and rebuild the APK file, the problem disappears.