- Newest
- Most votes
- Most comments
If you use API Gateway WebSockets, you do not need to maintain the connection in the Lambda function. The connection is handled by API Gateway. You do need to manager the connections in a DynamoDB table to know where to send messages to.
Saying that, I would recommend using AppSync Events instead. It is a serverless pub/sub system, that uses websockets. The clients subscribe on channels and they can publish to channels. A channel can be a group chat, so all participants will receive the messages, or it can be a specific user id, and only that user, that subscribed on that channel, will receive the messages.
For adding a real-time chat service to your existing serverless architecture, AWS Lambda with API Gateway WebSockets is actually well-suited for your use case, despite your concerns.
API Gateway's WebSocket APIs are designed to handle persistent connections while Lambda functions handle the business logic when messages are sent. This approach aligns well with your existing serverless architecture and maintains the benefits of serverless computing: reduced infrastructure management, automatic scaling, and pay-per-use pricing.
AWS provides a specific pattern for creating WebSocket chat applications using API Gateway, Lambda, and DynamoDB. DynamoDB in this context is primarily used to store connection IDs and user information, which is essential for managing WebSocket connections and routing messages to the right recipients.
While you mentioned reluctance to introduce DynamoDB, it's particularly well-suited for this purpose as it provides the low-latency, high-throughput storage needed for connection management. The serverless approach would reduce your overall maintenance workload compared to managing EC2 instances, as AWS handles the underlying infrastructure.
If your concern is about long-lived connections with Lambda, it's important to note that Lambda is only invoked when messages are sent or when connections are established/disconnected - not continuously while connections are idle. API Gateway manages the persistent WebSocket connections, while Lambda functions are only triggered for specific events.
This serverless approach would be more cost-effective for variable workloads and would integrate seamlessly with your existing serverless components, maintaining the benefits of reduced backend maintenance that you're already experiencing.
Sources
Seeking Advice on Serverless Architecture for an AI Chat App with Flutter Frontend | AWS re:Post
Create a websocket chat application with API Gateway - AWS Lambda
Serverless logic tier - AWS Serverless Multi-Tier Architectures with Amazon API Gateway and AWS Lambda

+1 for AppSync Events.