패키지, 저장소 및 앱 카테고리 검색

패키지 상세v3.0.2

eventflux

간편한 연결과 데이터 관리로 서버에서 전송하는 이벤트 스트림을 효율적으로 처리합니다.

AndroidiOSmacOSWindowsLinux
30일 다운로드
1.9만
좋아요
97
Pub 점수
160

패키지 설치

flutter pub add eventflux

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

EventFlux는 서버 전송 이벤트(SSE) 스트림을 효율적으로 관리하기 위해 설계된 Flutter/Dart 패키지입니다. SSE 엔드포인트에 대한 원활한 연결, 자동 재연결 처리 및 구조화된 데이터 처리를 제공하여 실시간 데이터 통신을 간소화합니다. 알림, 대시보드 또는 라이브 피드와 같이 실시간 업데이트가 필요한 애플리케이션에 적합하며, 최소한의 보일러플레이트로 신뢰할 수 있는 이벤트 전달을 보장합니다. Android, iOS, macOS, Windows, Linux 등 여러 플랫폼을 지원하여 크로스플랫폼 실시간 앱에 적합합니다.

사용 사례

  • 실시간 알림
  • 실시간 대시보드 업데이트
  • 스트리밍 데이터 피드
  • 채팅 애플리케이션
  • 모니터링 시스템

주요 기능

  • 자동 재연결
  • 스트림 라이프사이클 관리
  • 크로스 플랫폼 지원
  • 이벤트 처리를 위한 간단한 API
  • 내장 오류 복구

적합한 대상

  • 실시간 앱 개발자
  • 백엔드 통합 플러터 앱
  • 실시간 데이터 업데이트가 필요한 앱
  • 서버 전송 이벤트를 사용하는 팀
  • 다중 플랫폼 프로젝트

고려 사항

  • SSE 호환 백엔드 필요
  • 서버에서 클라이언트로의 스트리밍에 한정
  • 내장 인증 처리 없음
  • 네트워크 안정성에 따라 다릅니다
  • 복잡한 페이로드를 처리하기 위해 사용자 정의 파싱이 필요할 수 있습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

EventFlux

A Dart package for Server-Sent Events done right — WHATWG spec-compliant parsing, auto-reconnect with exponential backoff, request/response interceptors, mid-flight abort, idle timeout detection, and web platform support out of the box.

Platform Support

Android iOS Web MacOS Windows Linux
✅ ✅ ✅ ✅ ❓ ❓

Windows and Linux should work but haven't been battle-tested yet — PRs welcome if you get there first.

Features 🌟

  • 📜 WHATWG SSE spec-compliant event stream parsing with persistent lastEventId and retry: field support
  • 🔄 Auto-reconnect with linear or exponential backoff, random jitter, and configurable maxBackoff cap
  • 🔗 Interceptor chain — hook into request, response, and error lifecycle stages
  • 🛑 Mid-flight abort via a Future<void> trigger
  • ⏱️ Idle timeout detection — drops the connection if no data arrives within a configured duration
  • 🌐 Web platform support with CORS, credentials, and caching configuration via WebConfig
  • 🔍 Event filtering by type using response.where()
  • 📎 Multipart request support
  • 🏗️ Singleton (EventFlux.instance) and multiple independent connections (EventFlux.spawn())
  • 🔌 Pluggable HTTP clients via HttpClientAdapter
  • 🧠 Smart error classification — only 5xx, 408, and 429 trigger auto-reconnect

Migrating from v2? FYI. 🔄

Here

If you're upgrading from v2, here's what changed:

Breaking
  • onReconnect callback signature changed from () to (int attempt, Duration delay)
  • Default request headers now include Cache-Control: no-store
  • Minimum Dart SDK raised to >=3.4.0, Flutter >=3.0.0
  • webConfig is now required when running on web
New in v3
  • EventFluxStatus.reconnecting status value
  • originalError and stackTrace fields on EventFluxException
  • Request/response/error interceptor chain via interceptors parameter
  • Mid-flight abort support via abortTrigger parameter
  • Idle timeout detection via connectionTimeout on ReconnectConfig
  • maxBackoff on ReconnectConfig to cap exponential backoff
  • WHATWG-compliant SSE parser with persistent lastEventId, retry: field support, and U+2028 sanitization

Installation 📦

dependencies:
  eventflux: ^3.0.2-dev

Requires Dart SDK >=3.4.0 and Flutter >=3.0.0.

Usage 🔧

Basic Connection — Connect to an SSE endpoint in a few lines
import 'package:eventflux/eventflux.dart';

void main() {
  EventFlux.instance.connect(
    EventFluxConnectionType.get,
    'https://example.com/events',
    onSuccessCallback: (EventFluxResponse? response) {
      response?.stream?.listen((EventFluxData data) {
        print('Event: ${data.event}');
        print('Data: ${data.data}');
      });
    },
    onError: (EventFluxException error) {
      print('Error: $error');
    },
    onConnectionClose: () {
      print('Connection closed');
    },
  );
}
Auto-Reconnect — Exponential backoff with jitter and token refresh
import 'package:eventflux/eventflux.dart';

void main() {
  EventFlux.instance.connect(
    EventFluxConnectionType.get,
    'https://example.com/events',
    onSuccessCallback: (EventFluxResponse? response) {
      response?.stream?.listen((data) {
        print('Data: ${data.data}');
      });
    },
    onError: (error) {
      print('Error: $error');
    },
    autoReconnect: true,
    reconnectConfig: ReconnectConfig(
      mode: ReconnectMode.exponential,
      interval: Duration(seconds: 2),
      maxAttempts: 10,
      maxBackoff: Duration(seconds: 30),
      connectionTimeout: Duration(seconds: 60),
      onReconnect: (int attempt, Duration delay) {
        print('Reconnect attempt $attempt after $delay');
      },
      reconnectHeader: () async {
        String newToken = await refreshAccessToken();
        return {
          'Authorization': 'Bearer $newToken',
          'Accept': 'text/event-stream',
        };
      },
    ),
  );
}
Interceptors — Inject auth headers, log responses, suppress errors
import 'package:eventflux/eventflux.dart';
import 'package:http/http.dart';

class AuthInterceptor extends EventFluxInterceptor {
  @override
  Future<BaseRequest> onRequest(BaseRequest request) async {
    request.headers['Authorization'] = 'Bearer my-token';
    return request;
  }

  @override
  Future<StreamedResponse> onResponse(StreamedResponse response) async {
    print('Response status: ${response.statusCode}');
    return response;
  }

  @override
  Future<EventFluxException?> onError(EventFluxException exception) async {
    print('Intercepted error: $exception');
    // Return null to suppress the error, or return exception to propagate it
    return exception;
  }
}

void main() {
  EventFlux.instance.connect(
    EventFluxConnectionType.get,
    'https://example.com/events',
    interceptors: [AuthInterceptor()],
    onSuccessCallback: (EventFluxResponse? response) {
      response?.stream?.listen((data) {
        print('Data: ${data.data}');
      });
    },
    onError: (error) {
      print('Error: $error');
    },
  );
}
Abort a Connection — Cancel an in-flight request at any time
import 'dart:async';
import 'package:eventflux/eventflux.dart';

void main() {
  final completer = Completer<void>();

  EventFlux.instance.connect(
    EventFluxConnectionType.get,
    'https://example.com/events',
    abortTrigger: completer.future,
    onSuccessCallback: (EventFluxResponse? response) {
      response?.stream?.listen((data) {
        print('Data: ${data.data}');
      });
    },
    onError: (error) {
      print('Error: $error');
    },
  );

  // Cancel the connection at any time
  Future.delayed(Duration(seconds: 10), () {
    completer.complete();
  });
}
Web Platform — Configure CORS and credentials for browser SSE
import 'package:eventflux/eventflux.dart';
import 'package:flutter/foundation.dart' show kIsWeb;

void main() {
  EventFlux.instance.connect(
    EventFluxConnectionType.get,
    'https://example.com/events',
    webConfig: kIsWeb
        ? WebConfig(
            mode: WebConfigRequestMode.cors,
            credentials: WebConfigRequestCredentials.omit,
          )
        : null,
    onSuccessCallback: (EventFluxResponse? response) {
      response?.stream?.listen((data) {
        print('Data: ${data.data}');
      });
    },
    onError: (error) {
      print('Error: $error');
    },
  );
}
Multiple Connections — Run independent SSE streams in parallel
import 'package:eventflux/eventflux.dart';

void main() {
  EventFlux e1 = EventFlux.spawn();
  EventFlux e2 = EventFlux.spawn();

  e1.connect(
    EventFluxConnectionType.get,
    'https://example.com/stream-1',
    tag: 'Stream 1',
    onSuccessCallback: (EventFluxResponse? response) {
      response?.stream?.listen((data) {
        print('Stream 1: ${data.data}');
      });
    },
    onError: (error) {
      print('Stream 1 error: $error');
    },
  );

  e2.connect(
    EventFluxConnectionType.get,
    'https://example.com/stream-2',
    tag: 'Stream 2',
    onSuccessCallback: (EventFluxResponse? response) {
      response?.stream?.listen((data) {
        print('Stream 2: ${data.data}');
      });
    },
    onError: (error) {
      print('Stream 2 error: $error');
    },
  );

  // Disconnect both when done
  // await e1.disconnect();
  // await e2.disconnect();
}

Event Filtering — filter events by type using where():

response?.where('message').listen((data) {
  print('Message event: ${data.data}');
});
API Reference 📚
Connect

Connects to a server-sent event stream.

Parameter Type Description Default
type EventFluxConnectionType HTTP method (get or post) —
url String SSE stream URL —
onSuccessCallback Function(EventFluxResponse?) Callback on successful connection (required) —
header Map<String, String> HTTP headers {'Accept': 'text/event-stream', 'Cache-Control': 'no-store'}
onConnectionClose Function()? Called when the connection closes —
autoReconnect bool Auto-reconnect on disconnection false
reconnectConfig ReconnectConfig? Reconnection settings (required if autoReconnect is true) —
onError Function(EventFluxException)? Error callback —
body Map<String, dynamic>? Request body for POST —
files List<MultipartFile>? Files for multipart requests —
multipartRequest bool Send as multipart false
tag String? Debug tag (appears in logs) —
logReceivedData bool Log received SSE data false
httpClient HttpClientAdapter? Custom HTTP client —
webConfig WebConfig? Web platform configuration (required on web) —
interceptors List<EventFluxInterceptor>? Request/response/error interceptors —
abortTrigger Future<void>? Future that aborts the connection when completed —
ReconnectConfig
Parameter Type Description Default
mode ReconnectMode linear or exponential (required) —
interval Duration Base retry interval Duration(seconds: 2)
maxAttempts int Max reconnect attempts (-1 for unlimited) 5
maxBackoff Duration Cap for exponential backoff Duration(seconds: 30)
connectionTimeout Duration? Idle timeout — drops connection if no data received —
onReconnect void Function(int attempt, Duration delay)? Called on each reconnect attempt —
reconnectHeader Future<Map<String, String>> Function()? Async header refresh for reconnect —
EventFluxInterceptor

Subclass EventFluxInterceptor and override any of the following methods:

Method Signature Description
onRequest Future<BaseRequest> onRequest(BaseRequest request) Modify the request before sending (e.g., inject auth headers). Throw EventFluxException to abort.
onResponse Future<StreamedResponse> onResponse(StreamedResponse response) Inspect the response after receiving. Must not consume the stream body.
onError Future<EventFluxException?> onError(EventFluxException exception) Handle or suppress errors. Return null to suppress the exception.
EventFluxStatus
Value Description
connectionInitiated Connection process has started
connected Successfully connected to the event stream
reconnecting Auto-reconnect is in progress
disconnected Connection has been closed
error An error occurred during connection or disconnection
Disconnect
EventFluxStatus status = await EventFlux.instance.disconnect();

Returns a Future<EventFluxStatus> indicating the disconnection status.

Spawn
EventFlux instance = EventFlux.spawn();

Returns a new independent EventFlux instance for managing parallel SSE connections.

Contributors 💜

EventFlux wouldn't exist without these people who believed it could be better.

Peetee06 pedrohsampaioo krolmic FelippeNO jcarvalho-ptech aabegg jangruenwaldt glukose

Contributing 🤝

Contributions are welcome — open an issue or submit a pull request. Every bit helps.

License

Licensed under MIT.

카테고리

의존성

의존성 목록 2 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.