Imgkl / EventFlux

A Dart package for efficient handling of server-sent event streams with easy connectivity and data management.
https://pub.dev/packages/eventflux
MIT License
25 stars 8 forks source link

Reconnect does not work #18

Closed aabegg closed 3 months ago

aabegg commented 5 months ago

I have updated to version 2.0.0 and added the ReconnectConfig accordingly. When I now activate flight mode in the Android emulator, onError is triggered. If I then deactivate flight mode again, unfortunately nothing happens.

My ReconnectConfig is as follows

autoReconnect: true, // Keep the party going, automatically!
          reconnectConfig: ReconnectConfig(
            mode: ReconnectMode.linear,
            maxAttempts: -1,
            onReconnect: () {
              loggerNoStack.i('SSE RECONNECTION SUCCESS');
            },
          ),

Flutter 3.19.6

Imgkl commented 5 months ago

Hey @aabegg,

Thanks for trying out EventFlux and raising this issue, I'll look into this issue. I'll keep you in loop.

Imgkl commented 5 months ago

@aabegg just needed more info on this. Were you using EventFlux before? If yes, did the reconnect logic worked then?

aabegg commented 5 months ago

@Imgkl I previously used eventflux version 1.6.6. This worked perfectly. As soon as I activated flight mode, the connection was interrupted. This then triggered an error locally. When I then deactivated flight mode again and clicked on next in the VSCode debugger, eventflux immediately reconnected. This was also visible in the logs on the server.

In the new version of eventflux 2.0.0, the connection is interrupted when the flight mode is activated and no error is triggered (which is good in my case). But when I deactivate the flight mode again, nothing happens. Nothing can be seen in the logs on the server either.

Imgkl commented 5 months ago

Thanks for giving more context @aabegg. I'll sort this out soon. If this is a blocker for your developement, please use 1.6.6 directly.

eventflux: 1.6.6

Imgkl commented 5 months ago

Hey @aabegg,

I've identified the root cause of the reconnect issue in version 2.0.0. It relates to changes made in version 1.7.0 where we resolved issue #12 by properly disposing of the streamSubscription to avoid memory leaks.

Previously, the stream continuously listened for changes when network connectivity changed, which affected performance. The 1.7.0 update fixed this by ensuring streams were disposed of when not needed. However, this has unintentionally impacted the reconnect functionality in version 2.0.0, preventing automatic reconnection on network change.

I've pushed a fix, please check out the version 2.0.1. I'll close this issue post confirmation.

aabegg commented 5 months ago

Hi @Imgkl

I have now installed and tested version 2.0.1. Thanks for your work. I still have the problem that when I turn on the flight mode and then turn it off again, eventflux does not reconnect. Since version 2.0.1, eventflux tries to connect once according to the logs, but not after that.

What else I have noticed. As soon as I switch on flight mode, eventflux triggers an error. This is actually wrong from my point of view. eventflux has a callback function onError. It is actually right if this is triggered and not an additional error. If the error is to be passed on, the error could then be rethrown in the onError function.

Here is my current code how I use eventflux and below the corresponding logs from the Debug Console

EventFlux.instance.connect(
        EventFluxConnectionType.get, config.sseUri.toString(),
        onSuccessCallback: (EventFluxResponse? response) {
          loggerNoStack.i('SSE CONNECTION SUCCESS');

          response?.stream?.listen((data) {
          // processing data
          });
        },
        onError: (EventFluxException error) {
          // Oops! Time to handle those little hiccups.
          loggerNoStack.e('SSE CONNECTION ERROR: ${error.message}');
        },
        autoReconnect: true, // Keep the party going, automatically!
        reconnectConfig: ReconnectConfig(
          mode: ReconnectMode.linear,
          maxAttempts: -1,
          onReconnect: () {
            loggerNoStack.i('SSE RECONNECTION SUCCESS');
          },
        ),
        onConnectionClose: () {
          loggerNoStack.i('SSE CONNECTION CLOSED');
        },
      );
[EventFlux] ❌ Data Stream Listen Error: 200: ClientException: Connection closed while receiving data, uri=https://web4fm.ch/api/sse
I/flutter (28094): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (28094): │ ⛔ SSE CONNECTION ERROR: ClientException: Connection closed while receiving data, uri=https://web4fm.ch/api/sse
I/flutter (28094): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
[EventFlux] 🔄 Trying again in 2 seconds
[EventFlux] ℹ️ Connection Initiated
I/flutter (28094): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (28094): │ 💡 SSE RECONNECTION SUCCESS
I/flutter (28094): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter (28094): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
[EventFlux] ℹ️ Disconnecting
I/flutter (28094): │ ⛔ SSE CONNECTION ERROR: ClientException with SocketException: Failed host lookup: 'web4fm.ch' (OS Error: No address associated with hostname, errno = 7), uri=https://web4fm.ch/api/sse
I/flutter (28094): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
[EventFlux] ℹ️ Disconnected

It should also be noted here that the log “SSE RECONNECTION SUCCESS” is triggered although the flight mode is still activated. The whole log happens in activated flight mode. If the flight mode is deactivated afterwards, nothing is logged anymore

Hasilt commented 5 months ago

I'm also facing the same problem. If nobody working on this issue - I'll take a look

Imgkl commented 5 months ago

@Hasilt Sure, Please take a look.

MrChausson commented 4 months ago

I also have the same issue here is what I have seen:

Imgkl commented 4 months ago

Thanks for your input @MrChausson. I am actively looking into this issue.

MrChausson commented 4 months ago

Hey @Imgkl thanks for looking into that issue. Do you have an ETA of when it will be fixed ? I am planning on switching to EventFlux for a project in order to have sse auto-reconnect but I need to know if this will be working before the end of my sprint.

aabegg commented 3 months ago

Hi @Imgkl, i have created a pull request which fixes the error I described above.

Imgkl commented 3 months ago

Hey @aabegg, Thanks for this contribution.

I'll verify this and will push a version out in next 24hrs.

Imgkl commented 3 months ago

Closing this issue now. Thanks to @aabegg this issue has been resolved. Please update to the version 2.1.0.