AWS API Gateway - Websocket API | Develop and Publish Websocket API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to my channel so in today's video we are going to see how we can uh implement websocket uh using aws api gateway i'm going i'm going with the assumption that you know what is a websocket connection and what are the advantages of it so we are only we will straight away look into how we can implement websockets using api gateway okay so these are all the topics that we are going to discuss in this series of videos under this uh topic so in today's video we are going to see how we can create a simple chat application using api gateway websocket so this this is not going to be an end-to-end chat application rather just the back-end portion of it like how do we utilize websockets we will not have any front end for this as such and in the next lecture we will talk about what are the some of the major important metrics that you have to monitor related to api gateway as well as lambdas and in third lecture we are going to cover some of the aws limits that you have to know before you roll out your code into production and the last lecture would be on some of the best practices and anything else that i might miss out in the first three lectures all right so let's get started with our first topic today like how can we create a simple chat application with api gateway web sockets so this is how our application would look like so pardon me for coming up with such a lousy diagram but i thought i should just give you a high level overview and then i would like to spend more time in coding actually so as you can see we will have these we have to come up with these three or four lambdas that you see on the right of the screen that a connect lambda a giant lambda a published lambda and a disconnect lambda and our back end would be a dynamodb so we will save all the records into the dynamodb database these are all the various websocket routes that you see on to your left there would be a connect route a join router publish and a disconnect route if you think of this right like for example let's say you're creating a whatsapp group okay so first time when you create a group you would want to join a channel correct join the group and then when you send a message which is publish you want that message to be sent to everyone else in that group right and then when you disconnect that is when you exit the group everyone else either they can be notified or you can silently come out of it so that's that's the kind of parallel on what we are trying to build here using uh websockets okay now let's get into the coding portion of it so i have already written the code for the lambdas i'm going to walk you through like all the various steps that you have to know about while you come up with the lambdas so i this is a simple maven project here that we have so these are the some of the core dependencies that you have to import sdk core this is uh aws sdk api gateway management api this is required because you want to like talk to the clients like post data to the connections etc post some messages to the connections or manage the connections so this is why you need it and the dynamodb dependency is of course to save data into the dynamodb and these are the lambda java events this is basically whenever api gateway calls a lambda function you want to capture those events so that is why you need this dependency and this is also for the lambdas and the last few dependencies are for logging all right now let's check the first one which is the connect lambda okay so what this lambda is doing is it's it's pretty simple so if you can see it implements this interface request handler and this is the api gateway websocket event and this is the api gateway websocket response so this is the request and this is the response that it is returning so as you can see if this is the handle request method which you're overriding and see the request type this matches with what you have in here api gateway websocket event and the response that you're giving back is nothing but what you've defined here api gateway v2 websocket response so connect lambda is pretty simple you're not really doing anything you're just logging what is the connection id associated with the websocket connection so connection id is nothing but uh think of it like a primary key in a database like every connection would have a unique connection id so this would help us in like managing the connections like if you want to send out a message to a connection you would use this connection id to push a message to the client you're not really doing anything here you're just setting up a response with a status code of 200 which which means it's it's all good that you're successfully returning back a response so there's nothing much going on in here so this is our first lambda connect lambda right so if you go back to the diagram here this is our connect lambda so first user connects to an api gateway then we would invoke this connect lambda so don't worry about what's really going on with all the lambdas and how they are tied up to the api gateway i'll i'll walk you through all the steps in setting up the api gateway and integrating them with the lambdas for now we are just going through the lambda code for the various lambdas that we have the second one that we have is the default lambda this is basically if the route doesn't match to any of the existing ones it would fall back onto the default lambda again there's nothing much here you're just setting up a response status code of 200 and you're returning back a response this is again a similar pretty similar to connect lambda it implements the request handler interface this is from the lambda runtime if you see and the request and response is api gateway websocket event and websocket response so in our case for the disconnect lambda we are going to actually use uh the default lambda itself i'll tell you why and it's not it's not ideal for a production uh scenario but i'll i'll tell you what can be done okay the next one is join lambda so this is like you are joining a whatsapp group like some something like that like so you're trying to join a channel so what you're doing here is that and what is it that you receive here like what is it the client sending you he's sending you two items like what is the channel id like which channel do you want to join and what is the route associated so this route would be excuse me this route would be used by the api gateway to [Music] route the traffic to the appropriate lambda this is the channel id so what you're doing in the channel lambda is basically you get the connection id you get the channel and you save them into a database so i'll i'll explain you on how the database table would look like or maybe i can i can show it to you right now so if you go to dynamodb and let's go check the tables so this is this table this is a pretty simple table you have the table name is channel connections and if and it has only two fields basically one is the channel id this is what is the channel id and who are all the connections that are in that channel in this case if you see under channel one you have four different connections so think of it like there are four different users who are uh in that channel and similarly we have another channel called test and it has like three different connections tied up to the channel so this is a pretty simple table that's all to it there's nothing more so in this lambda what you're really doing is you're just saving that record of channel id and connection id you're just tying them up together into the channel connections table that's the drawing channel and if you want to look into the dynamodb code of it right so as you can see you're just creating a map of channel id and connection id and then you're creating an item so in dynamodb you call them as items and then you save the item to the table so it's stable.put item it's it's pretty simple um so let me know if i'm skipping over these items too fast because my intention is not to get into the lambda code or or the dynamodb code but if you feel like you would need more explanation into this dynamodb code or either the lambda code i'm happy to come up with another video and explain in detail okay um so yeah that's that's the join channel lambda again it's it's the same interface it's implementing the same interface as the connect lambda and the default lambda so once after you save it to the dynamodb table like the channel id in connection id you are returning back a response of 200 okay and the final lambda that we have today is the published lambda so this is like now you are in a group your friend is also in the same group and you now you want to post a message to everyone else in the group so you would call this published lambda so what this published lambda would do is let's see what is the payload that it is taking uh so it's taking the route this route is used by the api gateway to call the appropriate lambda i'll explain more about this route channel id is nothing but okay i to which channel do you want to publish the data to and what is the data that you want to publish so basically if you send this input it would take this data and it would send it would publish the data to all the users who are tied up to this channel okay and how is that done so what you're doing in in here is first you're getting all the channel connections associated with that channel id this is again a dynamodb call but it's pretty simple so you're just querying by the channel id so you're retrieving all the connections associated with this channel id and then you're returning back a list so once you have the list here you're looping through that you loop through that connection ids and then you push data to that connection so this is this is important because this is how you actually push a data to a websocket connection for an api gateway so you you create this request create this object called post to connection request if you post to connection request and this is if you want to see which jar it is in this is our api gateway management api jr it's it's part of this one you construct this request object with a connection id and what is the data that you want to send it or push to that connection so this data is nothing but whatever data that you received from from the clients i'm just calling it push data it has it's a pretty simple one it just has a string data in it so you're converting that into json and then you're pushing it out to this particular connection so post to connection request and then you have this api sync so this is nothing but so you are using aws client builder class you have the endpoint configuration here so this is hard coded ideally this would be this would be system dot system dot get paid env this is the lambda environment variable and then so what you're trying to do here is you're trying to first get okay to which api gateway should i is this connection associated with so you're constructing this api sync using this api gateway url uh with the standard configuration so how does lambda have the privileges to push it to api gateway i'll explain you that in within the next few minutes so you create this endpoint configuration and using this endpoint configuration you create this api gateway management api async object using a standard builder and then with endpoint configuration whatever you built it here you're building it and this is in the u.s west 2 region it can be any reason it's up to you but in my in my case this is in in the us west 2 region and this api gateway url if you want you can just hard code it if your api gateway is already in there uh if not you can ideally it this should be an environment variable but so that way even if your api gateway changes you just have to update your environment variable so you construct this uh api gateway management api sync and then using this object here you just say post to connection and pass this post to connection request object so this would post whatever data that you want to send to or to that particular connection associated with this connection id so what you're doing here is you're looping through all the connection ids associated with the channel and then you're just pushing out the data to it okay once we look at all the whole flow for i think you would we can come back to this again and then you'll have a much better understanding of what we are trying to do okay so i hope you understood uh the the lambdas that we have here so we have the connect lambda where you just establish a connection you don't do you know you're not really doing much here you're just printing out the connection id and then you're returning a response similarly default lambda you're not doing much just return a 200 response a join lambda is you just saving the channel id and connection to the dynamodb and when you come to the published lambda you're retrieving all the connections associated with the channel and then you're pushing out the publishing the data to those connection ids using the api gateway sdk all right now what would the next step that you would do here is basically you do a maven clean package and then you would generate a jar with with all the dependencies in it so you can see i've generated this jar with all the dependencies here so this would and then once you build this jar you can do a maven clean package and then it should you should be able to build this jar and once this jar is ready now let's see how our lambdas are designed okay let's go to lambda so as you can see i have these four lambdas here let's look into one of them so how you can do is you can say just say upload and then you would point to this jar and then you just click upload upload here or open in this case so that would upload your code and then it would build this connect lambda here okay my runtime is java 11 and then in the handler you are specifying okay which lambda so within the jar right we have four lambdas correct but in this case this particular lambda is the connect lambda so you are referring to that connect lambda here yes you get the whole path including the package name and then you specify what is the method name that is handle request here and in the configuration you don't have much going on except one important item that is the lambda rule so what is this lambda rule let's see so if you look into the permissions right so you are giving lambda the permission to post to a connection and this is required an ec to full access and api gateway access as well because you are talking to api gateway dynamodb access because you are saving and retrieving data from dynamodb and cloudwatch access because you would be gen pushing out logs to the cloud watch so you would create a role with all these permissions and you don't have to give full access you can restrict it to a minimum like depending on what operations that you're performing but just to keep it simple i gave complete access here so you give all these permissions to the role and then you would assign that role execution role to the lambda so then now lambda has all the privileges to either to talk to api gateway or to talk to the dynamodb without this privileges if you try to run it it would fail with with errors either to connect to the api gateway or to connect to the dynamodb all right so in a similar fashion you would consider other lambdas as well but for the published lambda let me show you the configuration here so in the configuration if you look into the environment variables so here is where you would specify the api gateway url so if you remember the code if you go to the publish lambda here at the beginning you are trying to first get the environment variable of the api gateway url so it would retrieve the api gateway url whatever you specify here and then it would uh when it is trying to post a connection this connection is tied up to this api gate this particular api gateway api okay now let's go to api gateway and then let's let's create one for the websockets so as you can see this is this is what you would click on websocket api click on build let's give some name to it my sorry my websocket api and then route selection expression so don't worry about it so for now just go with what you have here request dot body dot action i'll tell you what this is in detail in the next few minutes click on next and now so by default as you can see you have some routes defined here the connect route here is the description of what it is doing so this is the route triggered when a client connects to your api so this is the first route that gets triggered disconnect route is when either the server or the client if they close the connection then the disconnect route is triggered and the default route would be triggered if the route that you pass in your message if that does not match with any of the routes then it would the default route would be triggered and we can add custom routes as well so now let's add custom routes for two of our lambdas here join and publish right because we have a connect route we have a disconnect route but we don't have routes for join and publish right so let's add our route for join and let's add another route for publish okay next in here what you're trying to do is you're trying to attach a lambda with each of the routes so you're saying that your integration type is lambda and your coming up with uh like connect this is the lambda function that you have created there this is for join route okay uh let's go back one step and see if yeah so add connector out and disconnect route add default sorry i forgot to add so and now you go to the next one so now for the connect route you want to add an integration integration in our case is a lambda integration so what you're saying is when a connect route is called you want to invoke this lambda called connect lambda and when disconnect is called you want to invoke um not just for the sake of this example i'm calling default lambda ideally you want to come up with a disconnect lambda and then that disconnect lambda should basically uh remove the connection from the dynamodb table like all the associated channels and connections no channels for that connection you wanna basically it's a clean up after a connection is uh disconnected but in in our case we're just uh sending it to the default lambda you're not doing the cleanup so don't don't do this in your production code and for the default one you are again going to the default lambda and for the join one you want to select the join channel lambda and for publish you want to select the publish land right click on next this is the stage name so so as you can see stages are independently configurable environments so if you have you can come up with as many stages as you want i think there is a soft limit of i think then i don't remember exactly but so in our case i'm just giving it a name called production it can be any name here so you add a stage click on next and then just say create and deploy so you can see our api gateway is now set up it is ready so these are all the routes that we have connect disconnect join publish and default right so the ones that we have here i did not specify default yet but that is also one of the routes and now in stages is our production stage here when you click on this one you would see few important items here one what is the url that the clients have to use to connect to our websocket and this is another important one so you have to copy this url here connection url so this is to manage your connections basically and if you go back to the lambda right let's go back to the lambda here and let's go to the published lambda if you remember the environment variables we specified the api gateway url here right this is from my old old api gateway since we've come up with a new one here which has a different url that starts with nt so let's update this one you have to remove the add connections at the end you just have to leave the stage here and then save yeah so this is the url that the clients would use to connect and this is the url that our lambdas would use to push data to the connections um so there are some other settings here like some throttling limits which i will discuss in the in the next lectures um but yeah so basically that's that's about it like if you want to just get started so you have all the routes you have your stage you have your url now let's see how this works okay now let's go to uh terminal here and then so i i've tried this so what you can do is you can download this thing called ws cat it's it's it's it's a easy way to establish websocket connections so if you just google for ws cat and then if you install it uh you you can just do whatever i'm doing here so the command that you want to give here is wscat hyphen c and then this is the url you want to go back here and copy this url that you have here command c and then paste see now it got connected so now let's see what has happened okay now if you go into uh cloud watch and then you go to log groups and if you go to connect here and then you check here you should see so see this log here it says websocket connection id is this one so this is nothing but what we have in our code here in the connect lambda so you are specifying here with socket connection id so if you go back to api gateway right so now what has happened is when a user first tries to establish a connection like this using ws cad he tried to connect right in now the connect route got invoked automatically because that's the first route that will be invoked the connect route got invoked and then as you can see this connect route is calling our connect lambda here this is the initial integration that you have set up the connect lambda got invoked and within the connect lambda you just printed this line and then you return back a 200 response right now it is a success now let's see what do you have to do next now you want to join a channel right so for the join channel if you look into the request how it looks like it should have two fields one is what is the route in our case and what is the channel id correct so route would be join because you want to join a channel right how you would do that is action join and channel id let's give it a test one okay so what is this action here it looks weird right so if you go back to the api gateway here see the route selection expression here this is request dot body dot action so instead of this you can specify something else as well but whatever you specify here like instead of action if you just give route maybe then what you have to do here is instead of action you would say route column join that's that's uh that is the significance of this field call action so now you join this channel here and then this is the channel id now what do you want to do next next you want to publish some data to the channel right you go back here and then see okay what is the route for publish it's nothing but publish if you click on every route you would see which lambda it is calling so when when someone sends a message on the publish route that request would be sent to this lambda publish land and what is the data so now when you go to publish here and then check the publish object pretty simple it should have a route which will be used by the api gateway what is the channel id that you want to publish data to and what is the data that you want to publish right so this is how our it will look like first thing is action as you know it would be publish next thing is channel id which is nothing but test one and what is the data that you want to publish let's just say hi okay now let's see what has happened here ideally we should have received that high here but okay we got it uh i mean in in in a in a production case scenario you don't want this message to come back to the same user who has published it you would ideally filter out this particular connection id and then you would want to send it to everyone else in the group but since this is just for demo purpose even this user who published the message also received that message as well now let's create another uh user and then let's see if we want to if he receives that message or not okay so it's again ws cad hyphen c and then you want to get the url right so now go back to the stage here copy this url and paste so this guy is also connected now let's so the same process now he wants to join a channel so he would specify the channel id as i think we gave it test one right right test one uh so you go to test one and then he joined the channel now he wants to do a publish again so he would give action as publish uh channel id would be this one and data would be uh hey i am user 2 okay so as you expected this guy received the message let's see if the other guy also received the message or not see here so hey i'm user 2. now if this guy sends a message let's see if the user 2 receives it or not as well so it should be the same thing again you want to publish specify the same channel id again test one and what's your data your data is hi 2 i am user 1 okay so let's see if the second user got this message or not as you can see so now you can you understood the whole flow right like so this is this is like you are chatting with someone else so you established a websocket connection the user one also established a websocket connection and the channel is left open and as you are typing messages it's it's it's going on the channel so it's bi-directional so you can send messages and you can receive messages on the same channel there is no overhead of establishing a http connection every time you want to send out a message so the channel is kept open and then you would receive the messages but does this channel will it be open all will it always be open indefinitely no it it won't be so in case of api gateway websockets so the maximum idle connection timeout is 10 minutes so what it means is that if you leave the channel open and if you're not sending any messages on it that channel would get disconnected within 10 minutes and then when it gets disconnected right this is what you would see disconnected uh with the code of one zero zero one going up this basically means it it got disconnected because it is idle i'll get into the status code etc in in the later videos uh but yeah so that is one limitation like the idle connection timeout and the second thing is what is the maximum time for which a channel can be open for api gateway the maximum time limitation is two hours so that means if you if the channel is not idle like if you're constantly sending messages even then the maximum time a channel can be open is only for two hours it cannot live for more than two hours it will get disconnected at that two hour period it would be disconnected by the api gateway you would get a different status code then so now next question is how can you keep the channel open right like user may or may not uh send messages for two hours right so the solution for that would be you just have to clients can keep sending some sort of ping message to the to the server just to keep so whenever a client sends a ping message server can respond back something like a dummy uh message for it as long as you have this constant pings for maybe every 10 seconds or 30 seconds or one minute depending on the firewall settings of the clients or whatever right so pick some number and then as long as you have these ping messages going across the channel both the base your channel would be open for that maximum duration of uh two hours um so yeah that's that's pretty much it and then if you want to look into the log site you can let's check in dynamodb to see if we got a new channel right see this one here so we have test one channel which we just created and it has two connections uh that you have these are the two connections that you established in the two uh tabs in the terminal that we just saw uh so that's pretty much it uh for this video um let me know if you have any questions or or if you feel like there is if there are any if there is any information that i left out or if i went through too fast um i'm happy to cover it in my further videos so as i mentioned earlier in the next video let's look into what are the various what are the various metrics that you want to monitor uh some of the api gateway metrics as well as the lambda metrics that you have to keep in mind when you roll this up to production yeah let's let's look into that in the next video uh so that's all i have guys so hope you like my video uh i mean if let me know if you have any feedback for me and i would be happy to answer any questions or concerns thank you very much guys hope you like my video please do share and subscribe thank you
Info
Channel: Alternative Degree
Views: 302
Rating: undefined out of 5
Keywords: API Gateway, AWS, AWS Lambda, Websockets, #AWS #Websocket #API Gateway #Lambda, Websocket, Lambda
Id: 8AHvidF8-TA
Channel Id: undefined
Length: 41min 6sec (2466 seconds)
Published: Fri Apr 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.