Creating a Scalable Serverless Chat Application on AWS

Creating a Scalable Serverless Chat Application on AWS

Today I had built a AWS serverless services like AWS Lambda and API Gateway to build a chat application in the AWS cloud that can effortlessly scale to meet high demand.

Prerequisites

-An AWS account with an IAM user that has administrative permissions.

A basic understanding of a high-level programming language such as Python is recommended but not required.

Basic understanding of databases and APIs.

Grit and persistence.

Create A Lambda Function

Sign in to the AWS Management Console: Go to the AWS Management Console.

Open the Lambda console: From the AWS Management Console, open the AWS Lambda console by searching for “Lambda” in the services search bar.

Create three function:
Function:1
Click on the “Create function” button.
Choose “Author from scratch”.
Configure the following settings:
Function name: Send Message
Runtime: Python 3.x (choose the latest available version)
Role: Choose “Create a new role with basic Lambda permissions”
Click “Create function” to create your Lambda function.

Function:2
Click on the “Create function” button.
Choose “Author from scratch”.
Configure the following settings:
Function name: Connect
Runtime: Python 3.x (choose the latest available version)
Role: Choose “Create a new role with basic Lambda permissions”
Click “Create function” to create your Lambda function.

Function:3
Click on the “Create function” button.
Choose “Author from scratch”.
Configure the following settings:
Function name: Disconnect
Runtime: Python 3.x (choose the latest available version)
Role: Choose “Create a new role with basic Lambda permissions”
Click “Create function” to create your Lambda function.

Create an API

creating an API using API Gateway which is a managed service that makes it fun and easy for developers to create, publish, maintain, monitor, and secure APIs to back-end systems running on EC2, AWS Lambda or any publicly addressable web service

In the API Gateway console, click on the Build button under WebSocket APIs as shown below
You can create a route selection expression like $request.body.action to route messages based on their “action” attribute as we have done in our case. Then, you’d define route actions for different “action” values, such as:

Route Key: sendMessage
Integration: Lambda function to handle the messages
This way, incoming messages will be routed to the appropriate backend Lambda functions based on their “action” attribute. Now let’s go to add routes.



Add Code to the Lambda Function

Edit the function code:

Scroll down to the “Function code” section.
In the inline code editor, you will see a file named lambda_function.py. This file contains the handler code for your Lambda function.
Write your code: Replace the default code with the following code:
for Send Message

python
Copy code
import json
import urllib3
import boto3
client = boto3.client(‘apigatewaymanagementapi’, endpoint_url=”xxxxxxxxxx.com/production”)

def lambda_handler(event, context):
print(event)
#Extract connectionId from incoming event
connectionId = event[“requestContext”][“connectionId”]
#Do something interesting…
responseMessage = “I am fine”
#Form response and post back to connectionId
response = client.post_to_connection(ConnectionId=connectionId, Data=json.dumps(responseMessage).encode(‘utf-8’))
return { “statusCode”: 200 }
Save the changes: Click the “Deploy” button to save and deploy your code.

Make sure you replace “xxxxxxxxxx.com/production” in the code with the connection URL you copied earlier. Remove /@connections from the end of the URL as it is not needed.

code for connect and disconnect
import json

def lambda_handler(event, context):
print(event)
print(“****”)
print(context)
return { “statusCode”: 200 }

Testing our Send Message Route

To make sure our Lambda function is working as integration for our API and a client can send messages, we are going to test our send message route. So go back to PieSocket with the WebSocket connection still open and try sending a message in the format:
{
“action”: “sendMessage”,
“message”: “Hello, how are you?”
}


Please follow and like us:
Pin Share