cant connect to server sent events
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();
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
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.
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));
}
}
);