AWS WebSocket API (Real-time chat application using python)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another session in today's video we will discuss about websocket apis web circuit apis support two-way communication that is both the users and the back-end can send messages back and forth once connected so it's used in real-time applications like charts broadcast messages or real-time dashboards you can create websocket apis in aws using aws api gateway service the sample use case that we are going to see today is a real-time char application there are few components here first is obviously the api gateway service itself which is used to set up the application and that is connected to three different lambdas to perform three different actions to establish connection to send messages and finally to disconnect then we have a dynamodb table which keeps track of all the current connections now let's get into the aws console and get started with setting this up okay so we are in the aws console and first let's start by creating the dynamodb table we're going to call it as websocket connections and this is going to be used to store all the active connections and we are just going to store the connection id so that's going to be our partition key and these connection ids are auto generated values which are generated by the api gateway okay so while this is being created let's get over into lambda console and start creating our tree lambdas so the first lambda function is going to be web socket connect and this is going to be invoked whenever there is a new connection to our api gateway and i'm going to use python 3.9 that's the latest version supported by lambda and let's get into visual studio and see our code for websocket connect so this is using photo3 library and it's just doing a put item to dynamodb with a connection id so let's copy this over and paste it into the lambda board and deploy this and we are going to do two more things here um head over into configuration and environment variables we are going to create an environment variable to store the dynamodb table name so this way if you're going to change the table name then you need not touch the code it's just enough to change the environment variable that's always a coding best practice and the second thing which we're going to do is the permissions so we are going to set up some lambda role permissions in order for it to be able to put item into dynamodb table so when whenever you create a lambda function it automatically comes with the default rule which allows to add cloudwatch logs and we are going to add a dynamodb permission as well this is going to be specific to our to the table which we created so let's add the region of the table and as well as the table name so this is the arn of the dynamodb table and the action that we allowed is put item because that's the only action that we need for this particular dynamodb table okay so we are done with our first lambda we are going to say follow the same steps and create two more lambdas the second one is going to be websocket disconnect so this will be invoked whenever there is a disconnection request to the websocket api again this is going to be python 3.9 as well and the code for this particular lambda is going to invoke dynamodb again but it's a delete item so we are going to delete the connection id that's being disconnected let's paste it into the lambda code source and deploy this and repeat the two configuration steps the first one is going to be setting up the time environment variable websocket table and the table name and the second thing which we're going to do is update the permissions so under execution role policies and edit the policy and this time we are going to add dynmo db service again but delete item it's very important to have limited permissions to your lambda so that you are available what your lambda can or cannot do and it's always a good practice to have restricted permissions okay so we are done with our second lambda as well and moving on to our third and final lambda so that's going to be the main one which is going to be used to send the messages so we're going to call it as websocket send and again it's going to use python 3.9 create the function let's get into visual studio and have a quick look at the code uh so this is lengthier than the other two uh because it's going to do a scan of the dynamodb table get all the connection ids and i'm also going to get the uh api gateway api endpoint so these endpoint urls can be derived from the event message that is being passed into the lamda by the api gateway and we are going to go through each of the connection and post messages to all the connections which are stored in dynamodb table so this way we can ensure that all the connection all the action active connections receive the message let's paste this and deploy and under configuration we are going to repeat the same set up the environment variable for dynamodb table name and then we're going to update the permissions so here we need uh two different permissions uh as usual we need the dynamodb permission but this time it's going to be a scan so let's get into the role and into the policy and the policy so choose dynamodb as the servers and under action it's going to be scan and table it's a specific resource the region of the resource and the table name so this is going to be our first permission and the second one is going to be specific to the connections so this time the service is going to be execute api and we are going to allow the action callers manage connections so this will allow the lambda to post messages into all the connections and i'm selecting any of the api gateways within the account and because we haven't created an api gateway resource yet so once we have that in place you can come back and edit this these wild cards into the actual data all right so we have all the three lambdas and the last thing to do will be creating the api gateway itself okay let's get into the api gateway console and here you have different types of apis so in our use case it's going to be websocket api and we're going to give it a api name and then a route selection expression so this is how your api will know which route to redirect your request to so you have to mention the route name within the action okay so we are going to define three different routes today uh one for connection next one for disconnection and the last one for sending the message itself so send message is going to be a custom route and also you have an option to specify a default route so if your action doesn't match any of the connection routes then it will be redirected to the default route and we are attaching each route to the lambdas which we created so you have various options you can either create it integrated with a lambda or a mock or a mock api so in our case we are going to do all three routes to the three different lambdas that we created and i'm just going to call the stage as demo finally review all the details that is the api name and the route selection in the routes itself and the integrations and finally the stage okay create and deploy this will be ready in a couple of seconds okay so we have our api up and running so let's get into stages and if you you can even have multiple stages for the same api gateway endpoint and here copy the websocket url and let's see how that works so i'm using postman here you can either use postman or you can you even use any ws client that you prefer but in this case copy the url and head over to postman and connect it i'm going to do the same for and make establish two other connections so i'm just creating a new web socket connection every time here okay as you can see there are three different connections now and let's get into dynamodb and see if there are three different entries so let's go into websocket connections table and as you can see there are three different connections now so each one represents and one open connection from our postman ui here so now what we are going to do is send try sending a message so i'm going to send a message from our first connection so the message body should have something called action so this is the route name which we mentioned while creating the api gateway if you remember so the action should match the exact exact route name so in our case it's then message and then you have to specify the message itself so here i'm trying to just send hello from terminal one so let's just assume that this is terminal one and two and three there are three terminals so in the logs you can see there is a message being sent and if you go into terminal 2 and 3 you should be seeing those messages being received all right so it works so you can do the same from a different terminal as well because they are all interconnected so you can send a message from terminal 2 and that will be received by terminal 1 and 3. so you can have additional attributes here and then play around by sending only to specific terminals or only specific connections and you can um restrict from being able to send to different connections and you have various options here and i have disconnected one of the connections so if you come back into dynmodb you you'll see that that is gone right so it's that simple so this is one of the use cases within api uh gateway web sockets you you can do various other use cases with this like setting up real-time dashboards and also any real-time operations so if you have any questions around this or websocket apis in general please leave them in the comments below see you soon in the next video thank you
Info
Channel: ListenToLearn
Views: 5,398
Rating: undefined out of 5
Keywords: api gateway, aws api gateway, websocket api, aws websocket api, python, real time chat application, lambda
Id: qmi-FuEoK4c
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Tue Jun 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.