serverless-http icon indicating copy to clipboard operation
serverless-http copied to clipboard

Streaming response

Open hakog77 opened this issue 1 year ago • 5 comments

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?

hakog77 avatar Sep 11 '23 12:09 hakog77

I came here for the same question :)

johnimholawal avatar Sep 15 '23 21:09 johnimholawal

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

creativityjuice avatar Sep 18 '23 12:09 creativityjuice

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
}

jdrydn avatar Nov 10 '23 22:11 jdrydn

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!)

dougmoscrop avatar Nov 10 '23 22:11 dougmoscrop

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?

MNorgren avatar Jun 13 '24 13:06 MNorgren