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.

已提問 10 個月前檢視次數 1277 次
2 個答案
0
已接受的答案

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
專家
已審閱 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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南