Build Faster and Better Real-Time Apps with AWS

Sriramadasu Prasanth Kumar
4 min readJul 12, 2023

Real-time applications are characterised by server-initiated data pushes to clients, resulting in faster and more interactive communication between users and devices. Examples include chat apps, online games, social media feeds, and live sports updates.

In this blog post, we will dive into the utilisation of AWS services for constructing real-time applications using WebSockets. WebSockets facilitate bidirectional communication between a client and server over a single TCP connection. Compared to HTTP, WebSockets are more efficient for real-time data transfer as they eliminate the overhead of HTTP headers and support persistent connections.

AWS offers a range of services that can aid in the development of real-time applications using WebSockets.

Notable services include:

- Amazon API Gateway: A fully managed service that facilitates the creation, publication, maintenance, monitoring, and securing of APIs. API Gateway supports WebSocket APIs, enabling the creation of real-time applications that communicate with clients over WebSockets. API Gateway manages connection handling, routing, authorization, and scaling of WebSocket connections.

- AWS Lambda: A serverless computing service that allows code execution without the need for server provisioning or management. Lambda functions can be triggered by various events such as HTTP requests, messages from Amazon SQS or Amazon SNS, and changes in Amazon S3 buckets. Integration of Lambda functions with API Gateway WebSocket APIs enables the execution of custom logic when a WebSocket connection is established, closed, or receives a message.

- Amazon DynamoDB: A fully managed NoSQL database service offering fast and consistent performance with seamless scalability. DynamoDB can store and retrieve any amount of data and handle any level of request traffic. It can also store connection IDs of WebSocket clients, enabling targeted message delivery to specific clients or broadcasting messages to all clients.

- Amazon SNS: A fully managed pub/sub messaging service that enables message delivery to multiple subscribers. SNS allows messages to be published to topics, which can then be delivered to various endpoints such as email addresses, mobile devices, and Lambda functions. Integration of SNS with API Gateway WebSocket APIs facilitates the transmission of messages from SNS topics to WebSocket clients.

To exemplify the utilization of AWS services for building real-time applications with WebSockets, we will develop a simple chat app that enables instantaneous message sending and receiving. The chat app will encompass the following functionalities:

- Users can connect to the chat app through a web browser.

- Users can enter their name and join a chat room.

- Users can view messages sent by other participants in the same chat room.

- Users can send messages to the chat room.

The architecture of the chat app will resemble the following:

To construct the chat app, follow these steps:

1. Create a WebSocket API using API Gateway. Specify three routes: $connect, $disconnect, and onMessage. The $connect route triggers when a user establishes a connection to the WebSocket endpoint. The $disconnect route triggers when a user disconnects from the WebSocket endpoint. The onMessage route triggers when a user sends a message to the WebSocket endpoint.

2. Develop three Lambda functions: connectHandler, disconnectHandler, and messageHandler. The connectHandler function is activated by the $connect route and stores the connection ID and user name in a DynamoDB table called Connections. The disconnectHandler function is activated by the $disconnect route and removes the connection ID from the Connections table. The messageHandler function is activated by the onMessage route and parses the user’s message, publishing it to an SNS topic named ChatRoom.

3. Establish an SNS topic called ChatRoom and subscribe a Lambda function named broadcastHandler to this topic. The broadcastHandler function is triggered by messages published to the ChatRoom topic. It scans the Connections table and transmits the message to all WebSocket clients utilizing the API Gateway management API.

4. Create a web page that enables users to establish a connection to the WebSocket endpoint using JavaScript. Implement the WebSocket API to generate a WebSocket object and set up event listeners for open, close, message, and error events. Utilize the Bootstrap framework to design a user-friendly interface for the chat application.

You can access the code for the Lambda functions and the web page here.

Testing the Chat App

To properly test the chat application, it is necessary to deploy the WebSocket API by utilizing API Gateway and obtaining the WebSocket endpoint URL. Additionally, the web page must be uploaded to an S3 bucket and static website hosting must be enabled. Once these steps have been completed, the web page can be opened in multiple browsers in order to initiate a chat.

Conclusion

Throughout this blog post, we have explored the utilization of AWS services to construct real-time applications using WebSockets. We have successfully developed a straightforward chat app that permits users to send and receive messages in real time. The backend logic of the chat app was implemented through the use of API Gateway, Lambda, DynamoDB, and SNS. Furthermore, the frontend user interface of the chat app was created using JavaScript and Bootstrap.

We hope you found this tutorial enjoyable and informative. Should you have any questions or feedback, please do not hesitate to leave a comment below. Happy coding! 😊

--

--

Sriramadasu Prasanth Kumar

MLOps| Hybrid Cloud | DevOps | Hadoop | Kubernets | Data Science| AWS | GCP |