AWS Lambda request does not stream provided data to the requesting client

0

I have a lambda responsible for running a simulation and returning the result to the requesting client. The result is 1 to 2 mbs in size. I read that streaming the data to the client using stream responses is the most performant way to relay that size of data, so that is what I have been attempting to setup.

My code looks like this:

const { Readable } = require('stream');
const pipeline = require("util").promisify(require('stream').pipeline);

exports.handler = awslambda.streamifyResponse(async (event, responseStream, _context) =>
{
    // generate simulation result
    matchResult = fs.readFileSync("/tmp/MatchResult.json");
    const requestStream = Readable.from(Buffer.from(matchResult));
    await pipeline(requestStream, responseStream);
    responseStream.end();
}

The await pipeline(requestStream, responseStream); does get run but I am not seeing any indication of a message being received or any errors on the lambda side indicating something is not setup correctly.

This is the yaml for the function:

  RunSinulation:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      MemorySize: 4000
      FunctionUrlConfig:
        AuthType: AWS_IAM
        InvokeMode: RESPONSE_STREAM
      Policies:
        - AmazonAPIGatewayInvokeFullAccess
        - S3CrudPolicy:
            BucketName: !Ref Bucket
      Environment:
        Variables:
          WebsocketEndpoint:
            Ref: WebsocketAPI
          BucketName:
            Ref: Bucket
    Metadata:
      Dockerfile: Dockerfile.dockerfile
      DockerContext: ./images/RunSinulation/

I am using a C# WebsocketSharp as the client.

Am I missing something on the server side, or is there something special I need to do with the client to receive these messages? I appreciate any help that can be offered.

asked 2 months ago193 views
1 Answer
2
Accepted Answer

I am not sure what is your issue, however, 1-2MB you can just return from a function without streaming. You should use streaming if your response size is larger than 6 MB, or, if you want to minimize the Time To First Byte.

Also, streaming does not mean websockets. You should use a regular HTTP client to get the request, which should arrive in chunks.

profile pictureAWS
EXPERT
Uri
answered 2 months ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions