react-native - streaming response from Lambda with InvokeWithResponseStreamCommand

0

I have the following code in a react-native app that calls a Lambda in my AWS tenant. The Lamda fires just fine, all the console.logs come out correct (200, correct content-type, etc.), and I can also see the response from the Lambda in CloudWatch, and it is the expected result. I also have this code running in regular React, and it also works fine, and I get the expected result.

However, in react-native the response comes different. Based on this AWS documentation, there should be something in the EventStream (InvokeComplete, PayloadChunk), but based on the logs, I seem to get nothing.

import { Lambda, InvokeWithResponseStreamCommand, LambdaClient } from "@aws-sdk/client-lambda"
import "react-native-url-polyfill/auto";
import 'react-native-get-random-values'

async streamAWS(payload) {
        const lambda = new Lambda({
            region: 'us-west-1',
            credentials: {
                accessKeyId: 'abc',
                secretAccessKey: 'def'
            }
        });

        const response = await lambda.send(new InvokeWithResponseStreamCommand(
            {
                FunctionName: 'streamFunction',
                Payload: JSON.stringify({ payload })
            }
        ));

        console.log(`+++++ LAMBDA +++++`);
        console.log(response.StatusCode); // 200
        console.log(response.EventStream); // {"undefined": [Function anonymous]}
        console.log(JSON.stringify(response.EventStream)); // {}
        console.log(response.ExecutedVersion); // $LATEST
        console.log(response.ResponseStreamContentType); // application/vnd.amazon.eventstream
    }

This is the code I'm using to extract the streamed data. This code works fine in React web, but it doesn't work in React-Native

const lambdaResponse = await backendService.streamAWS("some parameter");
                const decoder = new TextDecoder("utf-8");

                for await (const event of lambdaResponse.EventStream) { // error here
                    const text = decoder.decode(event.PayloadChunk?.Payload);
                }

And this is the error I get from React-Native:

LOG response: {"$metadata":{"httpStatusCode":200,"requestId":"b8eee2d1-a8e2-404f-9df3-dccb4810fdcd","attempts":1,"totalRetryDelay":0},"ExecutedVersion":"$LATEST","ResponseStreamContentType":"application/vnd.amazon.eventstream","EventStream":{},"StatusCode":200} WARN Possible Unhandled Promise Rejection (id: 0): TypeError: Object is not async iterable TypeError: Object is not async iterable

And last, this is the code for the lambda I'm trying to test:

export const handler = awslambda.streamifyResponse(async (event, responseStream, _context) => {
  // As an example, convert event to a readable stream.
  responseStream.write("Hello ");
  responseStream.write("world ");
  responseStream.write("from ");
  responseStream.write("Lambda!");
  responseStream.end();
});
Carlo
asked a year ago323 views
1 Answer
0

You must configure your AWS Lambda Function to stream responses as follows:

https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html

AWS
vtjean
answered a year ago
  • Hi, thank you for the answer, but that doesn't seem to be the problem. The lambda streams fine in React Web, but when I execute the exact same code in React-Native, it doesn't work.

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