AWS HTTP API to WebSocket API Bridge / Async Lambda - amazon-web-services

I'd really appreciate any advice on the following. Building a real-time infrastructure for android mobile devices to connect to windows POS systems.
POS system will run a .NET app that will connect to AWS WebSocket API Gateway and register with a storeId.
Android device will call an AWS HTTP API Gateway endpoint to retrieve POS information from the store.
Here's the part I'm not sure about. I'd like the AWS HTTP endpoint to trigger a lambda function that will lookup the websocket connectionId from the storeId and send a message to the connected store and wait for a new message from the store. The store client would receive the websocket message, collect the info, then send it to dynamodb and then notify the original lambda function that the data is ready to send back.
Can lambda do that? Connect to another websocket api, wait for a message, then disconnect, and eventually return the original HTTP API request to the android client? Am I going down the wrong path?
Is there a better way for an AWS HTTP API endpoint to send a message to a websocket client and get a response?
My other approach was to have the android client connect via websocket as well. Send request messages back and forth via websocket, and then call http api to upload / download larger data payloads. But would still like to find some way to provide HTTP API to others in order to retrieve the store data via the websocket client.

Related

AWS APIgateway websockets - how to return initial information one time once successful connection

I have build a API gateway stack that allows clients to connect and push data into a kinesis data stream.
I have a custom authentication lambda that runs on the initial connection which correctly validates the user. The session information is then written into dynamoDB along with some other process data that I would like to return to the client before it the client starts to send data to websocket $default route.
I've added a lambda to the $connect route and tried in both the authorization and connect lambda functions to return data via headers, directly, and through the apigateway #connections method but none of these method work, and the connection isn't yet established to be able to send data back down to the client.
Is this possible? Or would the only way to achieve this be to delay the response through an SQS queue, or to have the client request the data in someway before it starts?

System design : AWS chat application with API Gateway / SQS - how does server send message to recipient?

This is a system design question about creating a messaging application (like WhatsApp or FB messenger).
I saw a video that had the users connected via websocket to the API Gateway , then API GW pushed the message onto SQS - which was then polled by the EC2 compute layer to process the message (store it in the db) and hopefully send the message back to recipient.
How can the backend ec2 / compute layer send the message to the recipient (Bob) ? Can it just call a route on the API Gateway and it would know the connection details of the recipient and where to send? Would there need to be an additional caching layer to store info about the connection details of every user? I'm newer to AWS so not sure how this is accomplished and whether you can call API GW to send back to a user.
Also if you know how group chat would work please share.
As mentioned in the documentation, your backend EC2 servers can send messages to the connected clients directly via the #connections API. This documentation page walks you through how to do that.
For this, you'll need to add the connectionId to the header. See this answer on how to do so.

How do I register custom information to be sent when $disconnect route is executed using a WebSocket API?

Is it possible with Websocket AWS API to register custom information to be sent when disconnect event is triggered by the client?
The connection can be closed by the server or by the client. As the
connection is already closed when it is executed, $disconnect is a
best-effort event. API Gateway will try its best to deliver the
$disconnect event to your integration, but it cannot guarantee
delivery.
I understand that $disconnect is a best-effort event and could not reach to the integration, but for most cases it probably will so I would like to be able to send some information to the lambda about the user or device that disconnects and not to make another database call to get userId/deviceId for a given connectionID.
I have checked the request context of disconnect event (which contains metadata such as sourceIp) but none of the information from there I find reliable to do this mapping.

Integrate aws websocket api with springboot application

**I have created realtime activities website.
currently my website is like this:
frontend page created websocket with endpoint url wss://xyz.com/chat/$scope.userId
In my backend i store a map table of userId-Websocketconnection
when anyone likes image of userId-5 , backend sends notification to websocketConnection whose userId is 5
But now i want to use Aws websocket api
i am confused at several points
how to send userId with websocket connection request to aws websocket api
using aws websocket api ,how can i store this connectionid and user id in my springboot database
when someone likes image of userId-5 , how will i send notification to websocket connection of userId-5
Is it possible using aws websocket api. Please help me
**
That's what exactly APIGateway websocket protocol offers.
You need to connect to websocket, then send a message (json) which has userid in it. Example message below.
{"action": "SUBSCRIBE","payload": {"userId":"123"}}
On APIGateway you can create SUBSCIRBE route and map it to backend endpoint as Integration request where you store connection id into database.
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-integration-requests.html
Whenever your spring boot application wants to push notifications, you can make use of AmazonApiGatewayManagementApi and broadcast the message.
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html

Should we connect to AWS SNS every time we send a push notification?

I am working on DJANGO server, creating a REST service. The purpose of the service is to send a push notification to the phone number mentioned in the request. I am using AWS SNS service for push notifications. I am establishing connection between my server and AWS every time there is a request to the server. My question is, can we establish a connection once the server is up, like opening a port as we do in a chat application? Or I should establish a connection every time?
I am using Boto package and below is my code
connection = SNSConnection(AWS_ACCESS_KEY,AWS_SECRET_KEY)
connection.publish(message=jsonMessage, subject=title, target_arn=endPoint, message_structure=structure)
The Client classes in the AWS SDK are just wrappers around other lower-level HTTP clients that interface with the HTTP REST methods of the AWS APIs. They are very light-weight so instantiating and destroying a Client instance on each HTTP request to your DJANGO app should be just fine -- you will incur little overhead. I wouldn't worry about shared state clients like you might utilize in Java or other multi-threaded application designs.