AWS Case Study 6 - Serverless Realtime Chat App

AWS Case Study 6 - Serverless Realtime Chat App

From static web pages to realtime apps?

During the early years of the world wide web, people didn't expect too much from web pages they visited.

They were feeling happy after they had found a web page containing some interesting piece of information.

That was around the year 2000.

2021

Now we are in 2021.?

Times have changed and now users expect much more from websites.

They don't just expect the web pages to look good, they expect them to be smart and interactive.

And that includes being realtime.

What is a real time web app?

Realtime web apps are nothing more than apps running on a web that can react to changes that happen anywhere in a connected application’s system — not just to actions initiated by the current user. One of such examples is an instant messaging app.

Instant messaging is in fact the most common type of realtime app that was ever created.

Number of messaging app users will reach 3 billion in 2022

According to the Statista, the number of messaging app users is predicted to reach 3 billion in 2022.?

What does that mean?

It means that without a doubt, people love instant messaging a lot.?

So why not create our own instant messaging app?

To push or to pull?

Building your own instant messaging web app can become a challenge.

First, you need to carry out the technological decision.

You need to decide how you are going to send the data from the server to the client and back.

There are multiple options to choose from, but in general all options are based on using one of these two approaches:?

  1. Client pull - a communication style where the initial request for data originates from the client and then is responded to by the server. Pull is used extensively on the Internet for HTTP page requests from websites.
  2. Server push - a communication style where the request for a given transaction is initiated by the server (publisher). It is contrasted with pull/get, where the request for the transmission of information is initiated by the client (receiver).?

Which one is better?

There are many discussions going on internet about this topic.

If your criterion is efficiency, then you should go for server push approach as as it provides a higher amount of efficiency compared to the client pull in which there is much higher HTTP request/response overhead for each message that is sent and received.

Realtime Chat App in AWS serverless?

Good news it that AWS provides you serverless webservices that can be used to address both client pull and server push approaches.??

If you want to implement an instant messaging app based on the client pull mechanism, the core technology that you are going to use is the Amazon API Gateway's REST API Endpoint.

If you want to implement the server push mechanism, then you are going to need the Amazon API Gateway's Websocket API Endpoint.

Server push based AWS serverless architecture

This is the example of how realtime chat app AWS serverless architecture may look like.

AWS serverless architecture of realtime chat app

/click here to display the scheme in bigger resolution/

It consists of 2 layers.

  1. Presentation layer is nothing else than just a static HTML page hosted at AWS inside AWS Lambda function and accessible via API Gateway's REST endpoint and Amazon CloudFront. This HTML page contains javascript code that asks user to enter his name and then initiates a Websocket connection to the API Gateway's Websocket API endpoint which is actually a part of the Realtime communication layer.
  2. Realtime communication layer handles all incoming and outgoing Websocket requests via the Websocket API endpoint. Its core is the Websocket API endpoint and 41 lines of source code long Lambda function written in Node.js 14:

Realtime communication layer - AWS Lambda function

Websocket API endpoint is integrated with AWS Lambda function on all of its 3 routes:

  • $connect - Lambda function triggered from this route stores Websocket connectionId in the DynamoDB table,
  • $disconnect - Lambda function triggered from this route removes Websocket connectionId from the DynamoDB table,
  • message - Lambda function triggered from this route receives a chat message, retrieves all connectionIds from DynamoDB table and sends the chat message to all clients (all connectionIds). Should any connectionId be invalid, it is removed from the DynamoDB table immediately.?

Final note

As you can see, my Realtime Chat App is from the feature perspective rather simple app.

Chat.Serverless.Click - AWS Serverless Realtime Chat App - screenshot

It doesn't store any chat message history so in order for others to see your chat messages you should ask them to join you to exchange messages in realtime.

Also what is needed to mention, that all users of chat share the same discussion room and chat mutually between themselves.?

But would it expect more from application created just during 1 single weekend?

You can still extend it by yourself - I enclose all of its source code, you can find the link below..

Frequently asked questions

1) Let's talk about the costs. How expensive is the usage of Websocket API endpoint?

Communication costs via the WebSocket API as of today are just 0.95 EUR per each 1 mil. of processed requests (sent/received messages) per month.??

2) Why is the presentation layer hosted at AWS Lambda and not e.g. at S3? S3 is perfectly suitable for hosting static HTML files.

You are right. Simple Storage Service (S3) can serve the same purpose here and even could make the overall architecture look even less complex.?

I chose AWS Lambda and API Gateway approach just to demonstrate 2 different types of API Gateway endpoints in 1 architecture and also to demonstrate the less common approach of hosting static files in AWS (internet is full of examples how to use S3 for hosting static content).??

3) What if some portion of my website visitors are using legacy internet browsers or are behind very restrictive firewalls that either block or make WebSocket communication unreliable? Such users won't be able to use my web app and chat in realtime.?

If this is your case and you want your realtime chat app be available to as many users as possible then you should consider implementing a fallback mechanism.?

Mechanism that upon detection of unreliable websocket connection automatically switches communication from server push to the client pull approach.

Sure, technologically it is much more difficult to implement - but definitely worth if you don't want to limit any of your users from chatting using your app.

4) Does any other cloud provider offer similar solution to the Amazon's API Gateway Websocket API???

Yes, for example, Microsoft Azure. You can use its service called SignalR.?

5) Can you provide full instructions including the source code how to setup and configure this Realtime Serverless Chat app on AWS?

Yes, feel free to download my instructions.

6. Can I contact you in case I have questions or get stuck with the solution implementation?

Don't hesitate and contact me. You can reach me via my LinkedIn profile - I accept all requests to connect and will be glad to discuss AWS architectures with you.

Anil Kumar Pandey

React.Js | Node.Js | AWS | Typescript | FullStack Lead Engineer at Innodeed Systems

2 年

Rastislav Skultety Thanks for sharing this with us, I just did it with in 30 mins and its working as expected and it would be great if you can help little bit more regarding following items. 1. Is there any demo with attachment as well? How can I implement it? 2. Can you please share some suggestion to store the chat messages into some other databases like RDS, MongoDB or Redis. I just want to see the integration other than DynamoDB with serverless architecture.

Twana Daniel

Chief Technology Officer (CTO)

3 年

Hi Rastislav Skultety thanks for the article, and we are facing a major issue while using the same architecture you have here. Mohinder Singh and number of DevOps engineers are working on same matter. Please see the open stack over flow question we opened on the issue for the same architecture you have https://stackoverflow.com/questions/70515744/when-delivering-a-message-to-api-gateway-using-web-sockets-aws-lambda-is-not-be - Can we have a 15mins tech session with you Rastislav Skultety and we will explain the issue live on the call. If yes then please use this link and book a slot with me and I will add Mohinder Singh and few more DevOps engineers in our company, all of which facing this issue on AWS Lambda. https://calendly.com/devcentrehouse/30-mins-with-twana?back=1&month=2022-01 Thank You, Twana.

回复
Mohinder Singh

Senior DevOps Engineer at Dev Centre House (DCH)

3 年

1. In aws websocket limit is 32kb and if we need E2E encryption it's not sufficient because payload size is more than 128kb+. 2. We are using the same architecture, but not getting messages in order.

回复

要查看或添加评论,请登录

Rastislav Skultety, MBA的更多文章

社区洞察

其他会员也浏览了