pratikbaid3 / flutter_client_sse

Dart package to help consume SSE API. Consumes server sent events by returning parsed model of the event, id, and the data
https://pub.dev/packages/flutter_client_sse
MIT License
40 stars 41 forks source link

Bug: Extraneous whitespace in event data and extra newline at the end #10

Open AilurusRP opened 1 year ago

AilurusRP commented 1 year ago

Bug description

There is extraneous whitespace at the beginning of each line of the event data retrieved from event.data property. I believe this is because the library treats the space between "data:" and the message body in SSE format as part of the message body.

In addition, there is an extra newline character at the end of the event data, which causes some parsing issues.

Steps to reproduce

Create an SSE server with Express.js that returns a text chunk as shown below:

  res.write(`data: part-1\n`);
  res.write(`data: part-2\n`);
  res.write(`data: part-3\n\n`);

Create an SSE client with flutter_client_sse :

SSEClient.subscribeToSSE(
    url: 'http://<My Server Url>/events', 
    header: {
        "Accept": "text/event-stream",
    }).listen((event) {
        event.data?.split("").forEach((element) {
            print("$element ${element.codeUnitAt(0)}");
    });
});

Then print each char and its ascii code of the event.data string after receiving data from the server:

I/flutter ( 1067): --SUBSCRIBING TO SSE---
I/flutter ( 1067):   32
I/flutter ( 1067): p 112
I/flutter ( 1067): a 97
I/flutter ( 1067): r 114
I/flutter ( 1067): t 116
I/flutter ( 1067): - 45
I/flutter ( 1067): 1 49
I/flutter ( 1067):  10
I/flutter ( 1067):   32
I/flutter ( 1067): p 112
I/flutter ( 1067): a 97
I/flutter ( 1067): r 114
I/flutter ( 1067): t 116
I/flutter ( 1067): - 45
I/flutter ( 1067): 2 50
I/flutter ( 1067):  10
I/flutter ( 1067):   32
I/flutter ( 1067): p 112
I/flutter ( 1067): a 97
I/flutter ( 1067): r 114
I/flutter ( 1067): t 116
I/flutter ( 1067): - 45
I/flutter ( 1067): 3 51
I/flutter ( 1067):  10

It can be observed from the output that there is an extraneous whitespace (ASCII code 32) at the beginning of each line, and an extra newline character (ASCII code 10) at the end of the entire message block.