sst icon indicating copy to clipboard operation
sst copied to clipboard

Feature Request: awslambda.streamifyResponse

Open linguofeng opened this issue 1 year ago • 20 comments

https://aws.amazon.com/cn/blogs/compute/introducing-aws-lambda-response-streaming/

linguofeng avatar Apr 14 '23 01:04 linguofeng

+1

ggallotti avatar Apr 14 '23 11:04 ggallotti

Any idea on how this can be solved? It seems perfect for prototyping the openAI streaming option. Is there an option to upload a custom lambda function to use the new streamifyResponse decorator?. Could not figure out a way from the samples. thanks

ladrians avatar Jun 30 '23 13:06 ladrians

We'll need to take a look into this. We haven't gotten to it yet.

jayair avatar Jun 30 '23 23:06 jayair

https://github.com/astuyve/lambda-stream I can use streamifyResponse feature with this package, but currently only using for Lambda Function. And you have to configure Invoke mode to RESPONSE_STREAM image

bimbeonguyen avatar Jul 27 '23 10:07 bimbeonguyen

as a heads up, i'm about to submit a PR for adding streaming to AstroSite; hadn't thought about how that might be generalized to Function much, but i know there are challenges there.

edit: PR submitted, happy to assist with applying this elsewhere if anyone is looking for it.

adamdottv avatar Jul 29 '23 00:07 adamdottv

@adamelmore I'm looking to apply this to SveltekitSite. Seems like SvelteKit handles SST out of the box, so I'm hoping this should be a similar process to your Astro PRs. Let me know if I'm missing something.

sdocquir avatar Oct 04 '23 22:10 sdocquir

I need similar support for React apps too. Anyone with any ideas ?

DanaBrev avatar Jan 05 '24 13:01 DanaBrev

@DanaBrev what kind of React apps?

jayair avatar Jan 06 '24 01:01 jayair

bum @jayair I'm looking for something similar, for a NextJS app, I'm already using NextjsSite constructor and I'm creating an Api construct that should stream down to the nextjs website the response. Is there workaround for this at the moment? Thanks

Gr33nLight avatar Jan 18 '24 17:01 Gr33nLight

Nextjssite should support streaming. But the API won't because AWS' API Gateway doesn't support streaming.

jayair avatar Jan 20 '24 01:01 jayair

Yea I figured, I made it work by calling the lambda endopint directly and with the lambda-stream package for local dev

Gr33nLight avatar Jan 22 '24 12:01 Gr33nLight

edit: PR submitted, happy to assist with applying this elsewhere if anyone is looking for it.

any plans on making SvelteKit support streaming? i found this repo but it seems like it remove a bunch of functionality (a lot of commented code): https://github.com/sdocquir/svelte-kit-sst-streaming

aleksasiriski avatar Mar 30 '24 18:03 aleksasiriski

Still waiting on AWS to fix streaming on Lambda. Apparently they are working on a fix.

jayair avatar Apr 03 '24 22:04 jayair

Still waiting on AWS to fix streaming on Lambda. Apparently they are working on a fix.

So currently it's not working for any service, not just SST?

aleksasiriski avatar Apr 04 '24 07:04 aleksasiriski

Yeah, got an update that they are rolling it out to all regions this week.

jayair avatar Apr 09 '24 00:04 jayair

Streaming works for me using this code, but it seems that is has some fixed amount of either bytes or seconds that it still does a buffer and afterwards start to actually stream.

export const handler = awslambda.streamifyResponse(
        async (event, responseStream, context) => {
            const httpResponseMetadata = {
                statusCode: 200,
                headers: {
                       //use what makes more sense to your useCase
                        //"Content-Type": "text/event-stream"
                        //"Content-Type": "text/plain"
                        "Content-Type": "text/html"
                }
            };
            responseStream = awslambda.HttpResponseStream.from(responseStream, httpResponseMetadata);
            const text = `<p>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas blandit mollis risus vel ornare. Maecenas suscipit mauris eget lacus efficitur, id tristique lorem sodales. Aliquam commodo purus lacus, et pulvinar diam mattis at. Etiam scelerisque non enim sed lobortis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Sed ipsum lacus, porttitor a tincidunt quis, mattis vel urna. Praesent facilisis risus augue, nec tristique tellus sodales in. Donec mattis massa nec enim laoreet, a porttitor tellus molestie. Phasellus justo libero, faucibus sit amet leo sit amet, cursus consectetur ligula. In dui diam, malesuada posuere justo eu, malesuada condimentum sem. Nullam nec ornare magna. Maecenas quis eros ut metus elementum vehicula a at erat.

Etiam molestie mollis ante. Aliquam mollis imperdiet diam at luctus. Integer leo dui, sagittis nec nibh luctus, porta cursus velit. Nullam nec posuere sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec congue cursus erat ut fermentum. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi viverra, nisi eget finibus rhoncus, mauris nunc tincidunt neque, a vestibulum elit magna id arcu.

Curabitur pellentesque mauris quis purus volutpat imperdiet. Ut nec risus felis. Ut in diam lorem. Cras ut risus quis odio aliquet vehicula. Phasellus porta eu magna eget pulvinar. Quisque laoreet nisl varius ultrices tincidunt. Nam scelerisque metus at metus aliquam bibendum. Pellentesque lorem mi, scelerisque sit amet sem ut, aliquam sollicitudin libero. Suspendisse a massa quis nibh viverra consectetur consequat a elit. Mauris at hendrerit augue, ut laoreet lacus. Phasellus dignissim erat vitae sollicitudin aliquam. Nunc et efficitur mi. Nunc sit amet semper est, a fringilla nisl.

Sed varius leo mauris, id dignissim magna scelerisque id. Maecenas bibendum neque at risus feugiat, sed consequat augue cursus. Vivamus lacinia augue risus, eu dapibus leo fringilla eget. Curabitur pharetra in arcu quis fermentum. Vivamus felis ipsum, maximus sed vestibulum vitae, sodales sed lorem. Maecenas a tellus nec dui scelerisque aliquam. Praesent pretium consequat leo, a tincidunt turpis feugiat et. In euismod ex nec elementum interdum.

Ut ac nunc non mauris mollis euismod vitae dignissim sem. Sed ut molestie dolor, id scelerisque odio. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Phasellus iaculis justo lacus. Duis congue ac magna vitae laoreet. Suspendisse lobortis facilisis tellus laoreet consectetur. Fusce rhoncus fermentum odio. In venenatis, sapien ac pharetra ullamcorper, est ipsum vestibulum turpis, ac tincidunt ante magna in nisl. Nam ultrices feugiat eros. Proin fermentum massa ut mauris fermentum, at rutrum elit maximus. Nullam dignissim nibh eget augue venenatis, id tincidunt elit efficitur. Nullam nec diam vitae mauris mattis venenatis. Morbi molestie nec sem eget dignissim. Praesent imperdiet magna purus, sit amet commodo mi cursus sit amet. Nam eget velit ac nulla bibendum varius ac lacinia neque.
<p>`;
            for (let i = 0; i < 100; i++) {
                responseStream.write(text);
                await new Promise(r => setTimeout(r, 1000));
            }
            responseStream.end();
        }
    );

aleksasiriski avatar Apr 23 '24 18:04 aleksasiriski

Yeah that's the bug. We checked today, it hasn't been fixed yet.

jayair avatar Apr 26 '24 23:04 jayair

Is there any place that the "bug" is tracked, like a Github issue on some repo?

aleksasiriski avatar Apr 26 '24 23:04 aleksasiriski

I'm not sure if there's one on the AWS side because it's an internal issue. But we personally check it every few days.

jayair avatar Apr 27 '24 00:04 jayair

Hoping that this will also become available for RemixSite 🤞🏻 I'd like to use Remix Streaming but it's unclear how to do so when deploying with SST/ if it is currently supported (works locally of course, but not with site server lambdas)

nickcreel avatar May 29 '24 19:05 nickcreel