dio icon indicating copy to clipboard operation
dio copied to clipboard

ServerSendEvent support for Flutter Web

Open kuloud opened this issue 2 years ago • 6 comments

Request Statement

When request for ServerSendEvent stream on TargetPlatform: Web, it response with a UnsupportedError: RawSocket constructor

  Stream<T> requestForServerSendEventStream<T>(String url, Map<String, dynamic> request,
      {required void Function(CancelData cancelData) onCancel}) {
    final controller = StreamController<T>.broadcast();
    final cancelData = CancelData(cancelToken: CancelToken());
    try {
      onCancel(cancelData);
      _dio
          .post(url,
              cancelToken: cancelData.cancelToken,
              data: json.encode(request),
              options: Options(responseType: ResponseType.stream))
          .then((it) {
        it.data.stream.listen((it) {
          // ...
        }, onDone: () {
          controller.close();
        }, onError: (err, t) {
          // ...
        });
      }, onError: (err, t) {
        // ...
      });
    } on DioException catch (e) {
      // ...
    }
    return controller.stream;
  }

Solution Brainstorm

No response

kuloud avatar Jun 10 '23 15:06 kuloud

How do you initialize dio? RawSocket is a dart:io class, which means it's unavailable on web.

ueman avatar Jun 19 '23 07:06 ueman

final dio = Dio(BaseOptions( sendTimeout: setup.sendTimeout, connectTimeout: setup.connectTimeout, receiveTimeout: setup.receiveTimeout));

got it, Is there a plan to support this feature on web?

kuloud avatar Jun 19 '23 12:06 kuloud

This library already supports web, so you actually shouldn't have any problem. Can you provide an example which reproduces this issue?

ueman avatar Jun 19 '23 13:06 ueman

This is a duplicate of #1740, as XHR requests do not currently support readable streams.

AlexV525 avatar Jun 19 '23 14:06 AlexV525

Also, I've been planning to add extra support for SSE as an adapter or so, and only for the Web platform.

AlexV525 avatar Jun 19 '23 14:06 AlexV525

We should really throw an UnsupportedException or something like that for this operation while it's not working. That would make things more obvious.

ueman avatar Jun 19 '23 14:06 ueman