Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help Wanted: Streaming Data chunks from a Shelf Server #375

Closed
mark-at-pieces opened this issue Aug 23, 2023 · 2 comments
Closed

Help Wanted: Streaming Data chunks from a Shelf Server #375

mark-at-pieces opened this issue Aug 23, 2023 · 2 comments

Comments

@mark-at-pieces
Copy link

Hey all,

First, thank you for the wonderful Dart package!

My goal is to have an endpoint that will accept a post body and then basically send incremental chunks of data as we complete the processing.

I found a couple ways to do, here is a mock'd example where I would return the controller.stream immediately and then process the chunks in the background incrementally send then as they finish.

Future<Response> _echoRequest(Request request) async {
    var controller = StreamController<List<int>>();

    // Start an async task to send chunks
    Future(() async {
      for (int i = 0; i < 10; i++) {
        // Send size of the chunk in hexadecimal followed by CRLF
        controller.add('${'Chunk $i'.length.toRadixString(16)}\r\n'.codeUnits);
        // Send the chunk followed by CRLF
        controller.add('Chunk $i\r\n'.codeUnits);
        // Delay to simulate processing
        await Future.delayed(Duration(seconds: 1));
      }

      // Final chunk (size 0) followed by CRLF, and then end with another CRLF
      controller.add('0\r\n\r\n'.codeUnits);
      controller.close();
    });

    return Response(200, body: controller.stream, headers: {
      'Transfer-Encoding': 'chunked',
      'Content-Type': 'text/plain',
    });
  }

I found, however that the chunks don't send over to the client untill the controller has been fully closed.

I'm wondering if I'm doing something wrong here and was wondering if you all could help me out.

Another solution that I know will work is just default to websockets, but figured I would ask anyways.

Thanks,
Mark

@kevmoo
Copy link
Member

kevmoo commented Aug 24, 2023

See the docs here: https://pub.dev/documentation/shelf/latest/shelf_io/shelf_io-library.html

WRT to shelf.io.buffer_output – closing as not a bug, but happy to offer guidance if you repyl.

@kevmoo kevmoo closed this as completed Aug 24, 2023
@mark-at-pieces
Copy link
Author

@kevmoo this worked great thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants