Back

React nativie - Realtime - support

  • 0
  • Web
  • Realtime
Rudra Kishore
1 Jun, 2023, 17:37

Whenever I use a real-time subscription in my react-native app, I get the below error, Can someone please help? WebSDK - V11.0.0 useEffect(() => { const unsubscribe = client.subscribe([ databases.${DB_CONSTANTS.DATABASE_ID} .collections.${DB_CONSTANTS.POST_LIKE_COLLECTION_ID} .documents], response => { console.log(response); })

TypeScript
return () => {
  unsubscribe()
}

}, []) Image

TL;DR
The user is encountering an error when using a real-time subscription in their React Native app. The error is related to the inability to access the getItem property of undefined, likely due to React Native not supporting LocalStorage. The user is looking for a workaround. Solution: Install the "local-storage-fallback" package and add the provided code snippet at the start of the app to mimic the localStorage behavior using a fallback.
Binyamin
1 Jun, 2023, 17:38

What is the error? You get any other error in the device logs?

Rudra Kishore
1 Jun, 2023, 17:40
Rudra Kishore
1 Jun, 2023, 17:41

getting TypeError - can't read the property getItem of undefined

Binyamin
1 Jun, 2023, 17:44

Mmm the error is comming from here https://github.com/appwrite/sdk-for-web/blob/master/src/client.ts#L268 And this is because ReactNative doesn't supports LocalStorage

Let me see if there some workaround it.

Rudra Kishore
1 Jun, 2023, 17:44

oh, Thank you

Binyamin
1 Jun, 2023, 17:46

When you running

TypeScript
console.log(window)

In your ReactNative app what are the results?

Rudra Kishore
1 Jun, 2023, 17:57

it is an [Object]

Rudra Kishore
1 Jun, 2023, 17:58

unable to print as string due to TypeError: cyclical structure in JSON object

Binyamin
1 Jun, 2023, 17:59

Good. You can try to mimic the local storage class.

Install this package https://www.npmjs.com/package/local-storage-fallback

Add something like this at the start of your app.

TypeScript
import storage from 'local-storage-fallback'

if (!('localStorage' in window)) {
  window.localStorage = storage;
}

That should add all the required functions for the window object to be able to handle requests to is localStorage property.

Binyamin
1 Jun, 2023, 17:59

Try and lmn

Rudra Kishore
1 Jun, 2023, 18:00

sure, Thank you so much

Rudra Kishore
1 Jun, 2023, 19:13

Thank you @Binyamin no more we are getting the error.

Binyamin
1 Jun, 2023, 19:14

👍

Rudra Kishore
1 Jun, 2023, 19:16

WARN Appwrite is using localStorage for session management. Increase your security by adding a custom domain as your API endpoint. - I think this will be fixed once we add custom domain

Binyamin
1 Jun, 2023, 19:20

Yes, and it's not fail-proof as you don't really have localStorage.

Is it in dev mode or in compiled? As your origin is going to change in the production mode.

Rudra Kishore
1 Jun, 2023, 19:21

yes, it is in dev mode

Binyamin
1 Jun, 2023, 19:48

I think it would be good to check if your react native app will persist your localstorage. For that, you can:

  • Compile your application to production and test to see if the login flow (for example) persist to work after "Launching -> Login -> closing the App -> Relaunching" If it worked in that use-case then you OK
  • If it didn't persist, then you can have a more complex solution using any package that persist data, And create a custom wrapper, like so:
TypeScript
if (!('localStorage' in window)) {
    window.localStorage = {
        getItem: (key) => {
            // TODO: return the key value
        },
        setItem: (key, value) => {
            // TODO: persist the key value
        }
    };
}
Rudra Kishore
1 Jun, 2023, 20:00

okay, I will check that

Rudra Kishore
1 Jun, 2023, 20:02

I tried before without this package the auth session is being persisted

Binyamin
1 Jun, 2023, 20:03

So it seems like you're good to go

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more