eventflux 2.0.1 eventflux: ^2.0.1 copied to clipboard
Efficient handling of server-sent event streams with easy connectivity and data management.
import 'dart:developer';
import 'package:eventflux/eventflux.dart';
import 'package:eventflux/models/reconnect.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () {
String url =
'https://api-dev.magnifi.com/realtime-portfolio/updates?authtoken=eyJraWQiOiJIMDVrRVNGU3lnS2tiYU5HcXRwWW1qV1I0UzBBOGw2Q2tCUUpkckhCa3R3IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULmlZNkx3VEpCSVFEajlFajhMQ092UDZtSUxYcUI2QWw2bjB6dUZnRzhFUm8ub2FyMWl5bno4czM5SzRsd1Y2OTciLCJpc3MiOiJodHRwczovL2Rldi5hdXRoLm1hZ25pZmkuY29tL29hdXRoMi9kZWZhdWx0IiwiYXVkIjoiYXBpOi8vZGVmYXVsdCIsImlhdCI6MTcxNDU5ODU2NSwiZXhwIjoxNzE0Njg0OTY1LCJjaWQiOiIwb2ExazR1eXYwNnR3bkFXODY5NyIsInVpZCI6IjAwdTFrNWJ5MGh4R1ZrcGZMNjk3Iiwic2NwIjpbIm9wZW5pZCIsIm9mZmxpbmVfYWNjZXNzIiwiZW1haWwiLCJwcm9maWxlIl0sImF1dGhfdGltZSI6MTcxNDU5ODU2NCwic3ViIjoib29kMTBAYXBleC5jb20ifQ.WtrFbhM7QVAgqWt5MQNpElQJvxydkQtYpZpznrDQxXblmlY3XazK9hoin3S3ge8fAINZNeVeFsIhBhfqGWnqozudF5iF1R22MwK5QZ5yvvORzW8C5PoUmGANBdxGgPcuhY5h3-uaQj6qI0NPEW4YKKpTkPG9vEVLpvPhVrhUCslyGKPd9VTlP5wgeukLBpn1h0WqVZc2Xzxq0LUHg3zTH09pWKpoDWvmCoP16caRDAfdzKS219NxoKzZ--4DVjJ72gHOQf9tEnbgRHA9kGspSZHidlxMZ41AI59iJYjDmfjVA2o49LutdA4k_PaKhhz7alVu_tJVHE0VH3lE96uZ5g';
try {
EventFlux.instance.connect(
EventFluxConnectionType.get,
url,
autoReconnect: true,
reconnectConfig: ReconnectConfig(
mode: ReconnectMode.linear,
maxAttempts: -1,
interval: const Duration(seconds: 5),
onReconnect: () => log('Reconnecting...'),
),
onSuccessCallback: (EventFluxResponse? response) {
if (response != null) {
if (response.status == EventFluxStatus.connected) {
response.stream?.listen((event) {
log('Recieved Event $event');
});
} else {
log('Error');
}
}
},
onError: (error) {
log('Error Message Logged from UI: ${error.message}');
},
onConnectionClose: () {
log('Connection Closed');
},
);
} catch (e) {
if (e is EventFluxException) {
log('Error Message: ${e.message}');
}
}
},
child: const Text("Connect")),
TextButton(
onPressed: () async {
try {
EventFluxStatus status =
await EventFlux.instance.disconnect();
log('Status: $status');
} catch (e) {
if (e is EventFluxException) {
log('Error Message: ${e.message}');
}
}
},
child: const Text("Disconnect")),
],
),
),
),
);
}
}