dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.71k stars 164 forks source link

Streaming response #289

Open hakog77 opened 9 months ago

hakog77 commented 9 months ago

Hey, what is your opinion about streaming response? AWS announced support for streaming response for AWS Lambda with URL.

The idea is that the handler function is wrapped by streamifyResponse function (provided by AWS):

exports.handler = awslambda.streamifyResponse(
    async (event, responseStream, context) => {
        responseStream.setContentType(“text/plain”);
        responseStream.write(“Hello, world!”);
        responseStream.end();
    }
);

More documentation from AWS: https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/

Is adding response streams something you would consider to support?

johnimholawal commented 9 months ago

I came here for the same question :)

creativityjuice commented 9 months ago

Hey, Thanks for your lib :+1: I would also need this feature to increase payloads sizes for some routes of my API.

jdrydn commented 7 months ago

After reviewing Introducing AWS Lambda response streaming, I've just read:

Writing the handler for response streaming functions differs from typical Node handler patterns. To indicate to the runtime that Lambda should stream your function’s responses, you must wrap your function handler with the streamifyResponse() decorator. This tells the runtime to use the correct stream logic path, allowing the function to stream responses.

... I think this means in order to support streaming, all responses would have to be "streamed" regardless if the response is a stream, something like:

import serverlessHttp from 'serverless-http';

 export const handler = awslambda.streamifyResponse(serverlessHttp(app, {
  streaming: true,
}));

And underneath, something like:

async (event, responseStream, context) => {
  responseStream.setContentType("application/json");
  responseStream.write(JSON.stringify(res.body));
  responseStream.end();
}

Even if the final response isn't a stream? I imagine that might be problematic?


Afterthought: It's a shame AWS didn't just iterate on their current APIGatewayProxyResult type, for example:

{
  statusCode: 200,
  headers: { ... },
  body: someReadStream,
  isReadableStream: true
}
dougmoscrop commented 7 months ago

I think not only this, but the Function URL itself is configured for streaming or not. I don't remember if the payload indicates this, otherwise, it has to be a configuration option.

(but yes, supporting streaming here, is something I definitely want to support!)

MNorgren commented 3 weeks ago

I am also needing to support streaming in my AWS Lambda. Preferably only one a specific endpoint.

Has anyone already put together a workaround to get this to work until it is supported by this package?