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.

질문됨 3달 전208회 조회
1개 답변
2
수락된 답변

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
전문가
Uri
답변함 3달 전
profile picture
전문가
검토됨 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠