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 Answers
0
Accepted Answer

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
EXPERT
Uri
answered 9 months ago
profile picture
EXPERT
reviewed a month ago
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.

answered 9 months ago

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