react-native-event-source icon indicating copy to clipboard operation
react-native-event-source copied to clipboard

cant connect to server sent events

Open kennethsimon opened this issue 5 years ago • 3 comments

if i console data from server , it doesnt work , please help my code is as below const getMessages = () => { console.log('ken') const eventSource = new RNEventSource('https://328f2c2d8b21.ngrok.io/sse2'); eventSource.addEventListener("message", (data) => { console.log(data.data); }); }

getMessages();

kennethsimon avatar Aug 07 '20 07:08 kennethsimon

which platform are you running this on?

i was able to get this library working on iOS, but not android

it seems like the xhr onreadystatechange callback on android isn't invoked when it receives data; it's only triggered when the server-side connection is closed

fkurniaw avatar Aug 30 '20 02:08 fkurniaw

Yep this does not work on Android unless the connection is closed on the server side. one approach with which this can be made to work is after we have sent the data that we want from the server we close the connection. But for that to happen correctly there needs to be some state management on the server side.

Or, if you can risk using native modules non-javascript based then there are plenty. I'm using: https://github.com/ExpandoPakistan/react-native-eventsource.

But there's a catch! these are not a pure Javascript implementations and may break if the repositories go un maintained.

TheSalarKhan avatar Oct 22 '20 02:10 TheSalarKhan

Try to disable Flipper network interceptor. Go to android/app/src/debug/java//ReactNativeFlipper.java and comment next lines of code:

  // try to comment this code
  NetworkingModule.setCustomClientBuilder(
    new NetworkingModule.CustomClientBuilder() {
      @Override
      public void apply(OkHttpClient.Builder builder) {
        builder.addNetworkInterceptor(new     FlipperOkhttpInterceptor(networkFlipperPlugin));
      }
    }
  );

thiago7almeida avatar Jun 15 '21 20:06 thiago7almeida