pratikbaid3 / flutter_client_sse

Dart package to help consume SSE API. Consumes server sent events by returning parsed model of the event, id, and the data
https://pub.dev/packages/flutter_client_sse
MIT License
42 stars 45 forks source link

Reconnect after server disconnect #6

Closed skaba closed 2 months ago

skaba commented 2 years ago

Currently when server closes connection stream is ended, can we make it reconnect?

pratikbaid3 commented 2 years ago

Hi @skaba. Thank you for the suggestion. Will work on it and update the package.

bmikaeli commented 2 years ago

Hello, do you have any update on this ?

MeowTechOpenSource commented 1 year ago

Hi, how can I catch error when the server disconnects or not avalible on connect

Imgkl commented 10 months ago

Hi, Any updates on this?

Imgkl commented 9 months ago

First of all, Thanks to @pratikbaid3 for this package. This issue led me to create my own package with better error handling and reconnection logic.

Check it out, EventFlux.

devprog777 commented 5 months ago

await Future.delayed(Duration(seconds: 5)); // wait be

devprog777 commented 5 months ago

Stream<Map<String, dynamic>> _connectToSse() async* { while (true) { try { final request = await http.Client().send(http.Request('GET', Uri.parse('http://your-sse-server.com'))); final response = await http.Response.fromStream(request);

    if (response.statusCode != 200) throw Exception('Failed to connect to SSE server');

    yield* utf8.decoder.bind(response.stream).transform(const LineSplitter()).map((line) {
      try {
        return jsonDecode(line);
      } catch (e) {
        print('Failed to decode JSON: $e');
        return null;
      }
    }).where((json) => json != null);
  } catch (e) {
    print('Connection to SSE server lost. Attempting to reconnect...');
    await Future.delayed(Duration(seconds: 5)); // wait before attempting to reconnect
  }
}

}