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.
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.
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:?
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.
/click here to display the scheme in bigger resolution/
It consists of 2 layers.
Websocket API endpoint is integrated with AWS Lambda function on all of its 3 routes:
Final note
As you can see, my Realtime Chat App is from the feature perspective rather simple app.
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.
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.
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.
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.