1

I have tried several different SSE-plugins to receive server-sent events in a Flutter Web app, and all of them can connect but I don't receive any events in Flutter. However, if I open DevTools in Chrome and go to Network -> EventStream, I see the events coming in from the server. What could be the reason I don't receive them in the app?

I have tried sse, sse_client and eventsource, but the result is the same in all of them.

Here's how I implemented the app using the sse_client plugin:

sseClient = SseClient.connect(Uri.parse('https://example.com/test-sse/index.php'));
print('sse connected');

sseClient.stream.listen(
    (event) {
      print('sse event: ${event.toString()}');
  },
  onError: (err){
    print('sse error');
  },
  onDone: (){
    print('sse closed');
  },
);

The only output I get is sse connected, and then nothing - even though the events keep coming in the DevTools Network/EventStream tab.

Here's my PHP test script:

<?php

ob_end_clean();

set_time_limit(60);

while (true) {
    // Headers must be processed line by line.
    header('Access-Control-Allow-Origin: http://localhost:56008');
    header('Access-Control-Allow-Credentials: true');
    header('Content-Type: text/event-stream; charset=utf-8');
    header('Cache-Control: no-cache');

    $data = [
        'message' => 'Event: server-time='.date('G:H:s', time()),
        'dummy' => str_repeat(' ',1024*64)
    ];
    print("id: ".microtime()."\n");
    print("event: ping\n");
    print('data: '.json_encode($data)."\n");
    print("\n");

    ob_flush();
    flush();
    
    sleep(1);
}

I have tried removing the id and the dummy etc., but the result is the same - the browser receives the events, but the app doesn't receive anything.

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • Facing the same issue. The http package (used by almost all the libraries for sse) does not recieve events in chunks. It receives in one go. If you close the connection server side, it gets all the events as one big chunk. Only happens in web. Desktop, iOS & Android fine. – Srikanth Sep 27 '22 at 03:55
  • https://github.com/dart-lang/http/issues/337 – Srikanth Sep 27 '22 at 03:58
  • Here's the culprit - "It is also unable to stream requests or responses; a request will only be sent and a response will only be returned once all the data is available." - [BrowserClient](https://pub.dev/documentation/http/latest/browser_client/BrowserClient-class.html) – Srikanth Sep 27 '22 at 09:45
  • what did you end up doing? – Srikanth Sep 27 '22 at 09:56
  • @Srikanth I ended up using SignalR instead, which is a Microsoft open source "protocol" to open a client-server connection. I'm using the signalr_netcore Flutter plugin, and on the server side an ASP.NET Core web application written in Visual Studio. I think this is a better solution than Server-Sent Events via PHP, which as far as I understand will eat up a lot of server resources when the number of simultaneous connections increase. – Magnus Sep 27 '22 at 10:49
  • Thanks. I guess we have no choice but to switch to websockets. – Srikanth Sep 27 '22 at 12:22

0 Answers0