sseRequestGetStream function

Stream<Map<String, dynamic>> sseRequestGetStream({
  1. required Uri uri,
  2. required String subName,
  3. Map<String, String>? headers,
  4. Encoding? encoding,
})

Creates an SSE GET request with the specified URI, headers and encoding.

Adds next default headers:

{"Cache-Control" = "no-cache", "Accept" = "text/event-stream"}

Example:

/// Obtain a [Stream] of events.
/// SubName is the subscription name, which is used
/// to distinguish connection events for multiple streams.
///
/// Nothing is send until the first listener is attached.
final streamGET = sseRequestGetStream(
  uri: Uri.parse('your_uri'),
  subName: 'name:1',
  headers: {'hello': 'world'},
);

final streamPOST = sseRequestPostStream(
  uri: Uri.parse('your_uri'),
  subName: 'name:2',
  headers: {'hello': 'world'},
  body: {'hello': 'world'},
);

/// Listens to the parsed SSE event stream.
final subscription = streamGET.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();

Implementation

Stream<Map<String, dynamic>> sseRequestGetStream({
  required Uri uri,
  required String subName,
  Map<String, String>? headers,
  Encoding? encoding,
}) {
  return SseRequest.get(
    uri: uri,
    headers: headers,
    encoding: encoding,
  ).getStream(subName);
}