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

Support for response streaming #154

Open
2 tasks done
serg06 opened this issue Apr 8, 2023 · 8 comments
Open
2 tasks done

Support for response streaming #154

serg06 opened this issue Apr 8, 2023 · 8 comments

Comments

@serg06
Copy link

serg06 commented Apr 8, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

AWS just released the ability to stream a Lambda function's response: https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/

It would be great if this framework supported it.

It requires wrapping the handler in streamifyResponse().

Motivation

This is an extremely useful feature which greatly decreases TTFB.

Example

// API which returns chunked data.
// Locally TTFB is 0, but on Lambda TTFB is 5s.
// Presumably because the handler is not wrapped in streamifyResponse.
fastify_streamtest.get('/chunked', (request, reply) => {
  // Create a buffer to hold the response chunks
  var buffer = new stream.Readable();
  buffer._read = () => {};

  // Generate 5 chunks with 1 second interval
  var count = 5;
  var emit = () => {
    var data = `Hello World ${count}`;
    console.log(`sending "${data}"`);
    buffer.push(data);

    count--;
    if (count > 0) {
      setTimeout(emit, 1000);
    } else {
      console.log('end sending.');
      buffer.push(null);
    }
  };

  emit();
  void reply.type('text/html').send(buffer);
});
@adrai
Copy link
Member

adrai commented Apr 8, 2023

image

@serg06
Copy link
Author

serg06 commented Apr 8, 2023

It should still work with direct Lambda URLs, no?

IMG_2489

@adrai
Copy link
Member

adrai commented Apr 8, 2023

Might work with Lambda function URLs, but...

beside that I don't know if the fastify inject function is able to handle chunked responses in a way to pass them to the awslambda.streamifyResponse 🤷‍♂️
Maybe @mcollina can confirm?

@mcollina
Copy link
Member

mcollina commented Apr 8, 2023

Given the current limitations I don't see this as such a big deal. It will definitely help Vercel.

I think it's possible to make a few modifications to inject to support this. However I don't have time to work on them right now.

@adrai
Copy link
Member

adrai commented Apr 8, 2023

thank you for the info

@Deanfei
Copy link

Deanfei commented Jul 21, 2024

Does this library support the lambda streaming response yet? That's the only main blocker to use it.

@adrai
Copy link
Member

adrai commented Jul 21, 2024

The Fastify inject function (and maybe also light-my-request), needs to be modified first.

@Deanfei
Copy link

Deanfei commented Aug 10, 2024

Any native support for the streaming now?

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

No branches or pull requests

4 participants