mrousavy / react-native-blob-jsi-helper

A React Native library for accessing an ArrayBuffer of a Blob instance.
MIT License
131 stars 8 forks source link

global.getBlobForArrayBuffer is not a function #13

Closed ionflow closed 10 months ago

ionflow commented 10 months ago

Does this error mean I'm not adding this library properly? I'm trying to use binary data (PNG image) sent through a websocket in an Image component in react native.

Error converting ArrayBuffer to Blob: [TypeError: global.getBlobForArrayBuffer is not a function (it is undefined)]

These are the steps I followed to install:

  1. npx expo install react-native-blob-jsi-helper
  2. eas build --profile development --platform ios to run a fresh build
  3. Installed new build on my iOS device
  4. Then ran npx expo start --dev-client locally
  5. Error happens during this code:
    try {
    const blob = getBlobForArrayBuffer(event.data);
    const url = URL.createObjectURL(blob);
    setMessages((prevMessages) => [...prevMessages, url]);
    } catch (error) {
    console.error("Error converting ArrayBuffer to Blob:", error);
    }
mrousavy commented 10 months ago

Maybe the JSI installer func did not get installed? Could you try setting a breakpoint on the native side

ionflow commented 10 months ago

I forgot to run npx pod-install. Now that error is gone.

As a side note, I don't think I'm using this correctly. Should this work?

const blob = getBlobForArrayBuffer(event.data);
const url = URL.createObjectURL(blob);
setMessages((prevMessages) => [...prevMessages, url]);
...
{messages.map((message, index) => {
  <Image
    key={`image-${index}`}
    source={{ uri: message}}
    style={{ width: 200, height: 200, resizeMode: "contain" }}
  />
})}

Currently it's just rendering a white box, but no errors.

ionflow commented 10 months ago

console.log("url:", url);

produces "url: blob:07A92E30-BD23-401F-83B4-771487CB1816?offset=0&size=undefined"

mrousavy commented 10 months ago

I don't know what event is, sorry.

ionflow commented 10 months ago

It's just a websocket MessageEvent. In my case event.data is an ArrayBuffer.