sse_request 0.2.1 copy "sse_request: ^0.2.1" to clipboard
sse_request: ^0.2.1 copied to clipboard

A dart package for consuming SSE (Server Sent Events) using configurable and extensible connection lifecycle and event handling controller

example/sse_request_example.dart

// Example usage of SseRequest.
// Demonstrates how to use SseRequest for both POST and GET requests.

// ignore_for_file: unused_local_variable

import 'dart:developer' as dev;
import 'package:sse_request/sse_request.dart';

void main() async {
  /// Create an [SseRequest] for a GET request.
  final getRequest = SseRequest.get(
    uri: Uri.parse('your_uri'),
    headers: {'hello': 'world'},
  );

  /// Create an [SseRequest] for a POST request.
  final postRequest = SseRequest.post(
    uri: Uri.parse('your_uri'),
    headers: {'hello': 'world'},
    body: {'hello': 'world'},
  );

  /// Obtains a [Stream] of events.
  ///
  /// First parameter is the subscription name, which is used
  /// to distinguish connection events for multiple streams.
  ///
  /// Nothing is send until the first listener is attached.
  final stream = getRequest.getStream('name:1');

  /// Listens to the parsed SSE event stream.
  final subscription = stream.listen(
    (event) {
      dev.log(event.toString());
    },
    onError: (e) {
      dev.log('Invalid SSE message: $e');
    },
  );

  // Demonstration delay.
  await Future.delayed(Duration(seconds: 10));
  dev.log('END');

  /// Don't forget to close the [StreamSubscription] to
  /// avoid memory leaks.
  subscription.cancel();
}
1
likes
160
points
4
downloads

Publisher

unverified uploader

Weekly Downloads

A dart package for consuming SSE (Server Sent Events) using configurable and extensible connection lifecycle and event handling controller

Repository (GitHub)
View/report issues

Topics

#sse #server-sent-events #configurable #http-stream

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

http, meta

More

Packages that depend on sse_request