How to get websocket response of client from AWS API Gateway

0

I am implementing an application with API Gateway, Websocket and Lambda/Python. However, I encountered an issue and would appreciate if anybody could provide a solution or workaround.

Below is the composition of my application:

  • web application: running in ec2, providing photo uploading function.
  • some facial recognition cameras: connected to API Gateway, acting as Websocket client.
  • Lambda #1: implemented as $connect route of API Gateway, saving the Websocket connection id in dynamo
  • Lambda #2: implemented as standalone Lambda, will be triggered when photo is uploaded on web application, get connection id from dynamo, call some Websocket api of the camera to upload photo to the camera.

The problem is that the result from the Websocket api of the camera is not received in Lambda #2.

The response has ResponseMetadata, with HTTPStatusCode 200 and content-length is 0.

I checked the camera and can tell the Websocket api is actually called, and using wscat as Websocket Server, the result from the camera can be received.

I guess maybe the result is received at API Gateway because the Websocket connection is between the camera and API Gateway, but not the camera and Lambda #2, so I should find some way to get the result out of the API Gateway. I checked the API Gateway document, Set up route responses for a WebSocket API in API Gateway seems similar but it is setting response to client side(camera), what I need is response to server side.

Another possibility is maybe the ApiGatewayManagementApi post_to_connection just not return the response. Document of Boto says RETURNS: None. Another method get_connection can get some response because the document says so.

2개 답변
1
수락된 답변

I am not sure I fully understand your setup.

With websockets in API Gateway that are two parts: The first is the one that API Gateway invokes when it receives a message from the client (either $connect when the client connects, or $default, for the messages themselves the client sends on the socket). The second is the way for the backend to send messages back to the client. This is done by using the post_to_connection API.

If you need to react to messages from the client, you need to connect Lambda #2 to the $default route in API Gateway.

profile pictureAWS
전문가
Uri
답변함 10달 전
profile picture
전문가
검토됨 4시간 전
profile picture
전문가
검토됨 2달 전
0

Hi, Uri thanks for your reply.

If you need to react to messages from the client, you need to connect Lambda #2 to the $default route in API Gateway.

I tried it and got the reply from client.

답변함 10달 전

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

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

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