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 个月前1278 查看次数
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
专家
已审核 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 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则