### Add eventflux Dependency to pubspec.yaml Source: https://pub.dev/packages/eventflux/install This is an example of how the eventflux dependency will appear in your pubspec.yaml file after running the add command. ```yaml dependencies: eventflux: ^2.2.1 ``` -------------------------------- ### Basic Flutter App Setup Source: https://pub.dev/packages/eventflux/example This is the main entry point for a Flutter application using EventFlux. It sets up the basic MaterialApp and Scaffold, directing users to the Readme for detailed usage instructions. ```dart import 'package:flutter/material.dart'; void main() { runApp(const EventFluxUsage()); } class EventFluxUsage extends StatelessWidget { const EventFluxUsage({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: Scaffold( body: Center( child: Text("Refer Readme for usage"), ), ), ); } } ``` -------------------------------- ### Import eventflux in Dart Code Source: https://pub.dev/packages/eventflux/install Include this import statement in your Dart files to start using the eventflux package's functionalities. ```dart import 'package:eventflux/eventflux.dart'; ``` -------------------------------- ### EventFlux Class - Spawn Method Source: https://pub.dev/packages/eventflux Returns a new instance of EventFlux, used for managing multiple SSE connections. ```APIDOC ## POST /spawn ### Description Returns a new instance of EventFlux, used for having multiple SSE connections. ### Method POST ### Endpoint /spawn ### Parameters This method has no parameters. ### Response #### Success Response (200) - **EventFlux** (EventFlux) - A new instance of the EventFlux class. ``` -------------------------------- ### Manage multiple parallel SSE connections Source: https://pub.dev/packages/eventflux Use EventFlux.spawn() to create independent instances for handling multiple concurrent SSE streams. ```dart import 'package:eventflux/eventflux.dart'; void main() { // Create separate EventFlux instances for each SSE connection EventFlux e1 = EventFlux.spawn(); EventFlux e2 = EventFlux.spawn(); // First connection - firing up! e1.connect(EventFluxConnectionType.get, 'https://example1.com/events', tag: "SSE Connection 1", // Optional tag for debugging, you'll see this in logs onSuccessCallback: (EventFluxResponse? data) { data.stream?.listen((data) { // Your 1st Stream's data is being fetched! }); }, onError: (oops) { // Oops! Time to handle those little hiccups. // You can also choose to disconnect here }, ); // Second connection - firing up! e2.connect(EventFluxConnectionType.get, 'https://example2.com/events', tag: "SSE Connection 2", // Optional tag for debugging, you'll see this in logs onSuccessCallback: (EventFluxResponse? data) { data.stream?.listen((data) { // Your 2nd Stream's data is also being fetched! }); }, onError: (oops) { // Oops! Time to handle those little hiccups. // You can also choose to disconnect here }, autoReconnect: true // Keep the party going, automatically! reconnectConfig: ReconnectConfig( mode: ReconnectMode.exponential, // or linear, interval: Duration(seconds: 5), maxAttempts: 5, // or -1 for infinite, reconnectHeader: () async { /// If you want to send custom headers during reconnect which are different from the initial connection /// If you don't want to send any headers, you can skip this, initial headers will be used // Your async code to refresh or fetch headers // For example, fetching a new access token: String newAccessToken = await fetchNewAccessToken(); return { 'Authorization': 'Bearer $newAccessToken', 'Accept': 'text/event-stream', }; }, onReconnect: () { // Things to execute when reconnect happens // FYI: for network changes, the `onReconnect` will not be called. // It will only be called when the connection is interupted by the server and eventflux is trying to re-establish the connection. } ), ); } ``` -------------------------------- ### EventFlux Class - Connect Method Source: https://pub.dev/packages/eventflux Connects to a server-sent event stream using various configuration options. ```APIDOC ## POST /connect ### Description Connects to a server-sent event stream. ### Method POST ### Endpoint /connect ### Parameters #### Query Parameters - **type** (EventFluxConnectionType) - Required - The type of HTTP request (GET or POST). - **url** (String) - Required - The URL of the SSE stream to connect to. - **header** (Map) - Optional - HTTP headers for the request. Default: `{'Accept': 'text/event-stream'}` - **onConnectionClose** (Function()?) - Optional - Callback function triggered when the connection is closed. - **autoReconnect** (bool) - Optional - Whether to automatically reconnect on disconnection. Default: `false` - **reconnectConfig** (ReconnectConfig?) - Optional - Configuration for auto-reconnect. If `auto-reconnect` is true, this is required. - **onSuccessCallback** (Function(EventFluxResponse?)?) - Optional - Callback invoked upon successful connection. - **onError** (Function(EventFluxException)?) - Optional - Callback for handling errors. - **body** (Map?) - Optional - Optional body for POST request types. - **files** (List?) - Optional - Optional list of files to send with the request. - **multipartRequest** (bool) - Optional - Whether the request is a multipart request. Default: `false` - **tag** (String?) - Optional - Optional tag for debugging. - **logReceivedData** (bool) - Optional - Whether to log received data. Default: `false` - **httpClient** (HttpClientAdapter?) - Optional - Optional Http Client Adapter to allow usage of different http clients. ### Response #### Success Response (200) - **EventFluxResponse** (EventFluxResponse?) - Encapsulates the response from EventFlux operations. #### Error Response - **EventFluxException** (EventFluxException?) - Custom exception handling for EventFlux operations. ``` -------------------------------- ### Connect to a single SSE stream Source: https://pub.dev/packages/eventflux Use the singleton EventFlux instance to establish a single SSE connection with optional multipart support and automatic reconnection configuration. ```dart import 'package:eventflux/eventflux.dart'; void main() { // Connect and start the magic! EventFlux.instance.connect( EventFluxConnectionType.get, 'https://example.com/events', files: [ /// Optional, If you want to send multipart files with the request ], multipartRequest: true, // Optional, By default, it will be considered as normal request, but if the files are provided or this flag is true, it will be considered as multipart request onSuccessCallback: (EventFluxResponse? response) { response.stream?.listen((data) { // Your data is now in the spotlight! }); }, onError: (oops) { // Oops! Time to handle those little hiccups. // You can also choose to disconnect here }, autoReconnect: true // Keep the party going, automatically! reconnectConfig: ReconnectConfig( mode: ReconnectMode.linear, // or exponential, interval: Duration(seconds: 5), reconnectHeader: () async { /// If you want to send custom headers during reconnect which are different from the initial connection /// If you don't want to send any headers, you can skip this, initial headers will be used // Your async code to refresh or fetch headers // For example, fetching a new access token: String newAccessToken = await fetchNewAccessToken(); return { 'Authorization': 'Bearer $newAccessToken', 'Accept': 'text/event-stream', }; }, maxAttempts: 5, // or -1 for infinite, onReconnect: () { // Things to execute when reconnect happens // FYI: for network changes, the `onReconnect` will not be called. // It will only be called when the connection is interupted by the server and eventflux is trying to reconnect. } ), ); } ``` -------------------------------- ### Add eventflux Dependency with Flutter Source: https://pub.dev/packages/eventflux/install Use this command to add the eventflux package to your Flutter project. It automatically updates your pubspec.yaml file. ```bash $ flutter pub add eventflux ``` -------------------------------- ### EventFlux Class - Disconnect Method Source: https://pub.dev/packages/eventflux Disconnects from the SSE stream. ```APIDOC ## POST /disconnect ### Description Disconnects from the SSE stream. ### Method POST ### Endpoint /disconnect ### Parameters This method has no parameters. ### Response #### Success Response (200) - **EventFluxStatus** (EventFluxStatus) - Indicates the disconnection status. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.