Search code examples
amazon-web-serviceswebsocketaws-api-gatewayamazon-sqssystem-design

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.

enter image description here


Solution

  • 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.