sse_parser 0.1.0 copy "sse_parser: ^0.1.0" to clipboard
sse_parser: ^0.1.0 copied to clipboard

A minimal, dependency-free, spec-compliant Server-Sent Events (SSE) parser for Dart and Flutter, with push and stream APIs. Built for LLM token streaming.

example/sse_parser_example.dart

import 'dart:async';

import 'package:sse_parser/sse_parser.dart';

/// Two ways to use the parser.
Future<void> main() async {
  // 1. Push API — feed decoded text chunks, get events via a callback.
  final parser = SseParser(
    onEvent: (event) => print('push  -> event=${event.event} data=${event.data}'),
    onComment: (comment) => print('push  -> comment "$comment"'),
  );
  parser.feed('event: greeting\n');
  parser.feed('data: hel'); // a chunk boundary mid-token is fine
  parser.feed('lo\n\n'); // blank line dispatches the event
  parser.close();

  // 2. Stream API — typical for an HTTP response body (Stream<List<int>>).
  final byteStream = Stream<List<int>>.fromIterable([
    'event: response.output_text.delta\n'.codeUnits,
    'data: {"delta":"Hi"}\n\n'.codeUnits,
    'data: [DONE]\n\n'.codeUnits,
  ]);

  await for (final SseEvent event in byteStream.parseSse()) {
    if (event.data == '[DONE]') break;
    print('stream -> event=${event.event} data=${event.data}');
  }
}
0
likes
160
points
124
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A minimal, dependency-free, spec-compliant Server-Sent Events (SSE) parser for Dart and Flutter, with push and stream APIs. Built for LLM token streaming.

Repository (GitHub)
View/report issues

Topics

#sse #server-sent-events #eventsource #streaming #llm

License

MIT (license)

More

Packages that depend on sse_parser