How To Build A Chat And Data Feed With WebSockets In Golang?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone in this video I'm going to teach you how to make your own websocket server in golang we are going to make a simple simple web chat application and after that we are going to make some kind of a feat where people can subscribe to and then you just dump some real-time data into everyone that subscribed over the web sockets uh to the browser or or other clients before we continue you know the drill if you're not subscribed to the channel consider subscribing give me a thumbs up leave some questions in the comments and jump into my Discord Community let's go alright so the first thing we're gonna do is what we always do is basically make a type and it's going to be a server there's going to be a structure uh and we're going to say we're gonna actually gonna maintain some connections right and that's going to be a map of uh Pointer's web socket dot connection and we're going to say boo there are a lot of ways you can do this you could also make a map of strings which is going to be the remote edit of that connection to the pointer of the website connection but hey a lot of things whatever you prefer I'm gonna do it this way for now so we're going to have a map of connections because a lot of people are go a lot of clients can basically connect uh subscribe to our websocket Handler and then we are going to send some data over it right very important is if I'm going to save this it's probably going to use this gorilla websocket we are not going to use gorilla we are going to use some standard Library stuff we're going to say golang um let me delete this Shenanigans I think it's goaling net X golang.org let me see it's going to be going.org okay cool so I'm going to use golang org um net it's xnet and the reason why I'm doing this is because it's uh this is a standard Library Edge package from going itself but most of the time this will not be installed so you need to go get this uh baby you need to go get this baby real quick for you so golang or xnet's websocket that's what we need that's what we're gonna use uh of course gorilla and all these other websocket packages are much more in-depth but hey you don't need all that stuff it's very important to learn these things from scratch all right so we have this thing then we're going to say funk new server uh like this it's going to return a pointer to a server we're gonna return what's going on here we're going to return a surface like this and then we're gonna make the Cons we're gonna make this uh into a map of pointer to web sockets .com and boo just like that right cool the next thing we need to do is make a Handler right so we're going to say s servers we're going to say handle um WS or something and we're going to make another this is for the chat and we're going to make another handle for our feet just add it as after this so we're going to say this needs to be a WS and it's going to be a pointer to a web socket.com no errors the first thing we're going to do is we're going to say print Allen you're going to say a new incoming connection from clients right and then we're going to say this and we're going to put it put out the remote adder what's going on is a remote address just like that all right uh the next thing we're going to do is we're gonna say s-cons right because you need to understand that each time the client is going to connect to to our Handler you will see uh what how is it going how that is gonna happen we're going to receive that connection and that we're going to need to maintain we need to keep track of that connection so if you need to do a broadcast uh we need to use all these connected connections and we need to write to them right you're going to see how that's going to work so we're going to say s cons WS is going to be true in this case right and you need to be careful because maps and golden are not concurrent safe so actually we should use some kind of a mutex but for the sake of this demonstration I'm not going to do it but hey keep in mind prediction stuff you need to have a mutex to make sure we yeah don't have any erase conditions or something in this case it's going to be not always needed but hey just do it so we have this uh thing and then we're going to say as a read loop we're going to make a read Loop because what we're going to do is for each connection we're gonna basically read from we're gonna listen to messages they sent so we can react and all that stuff if needed so we're going to do a re-loop of this connection and we're going to say we're going to make this function real quick we're going to say servers read Loop WS pointer to web sockets.com just like that you're gonna say four right because it's going to be a loop then we're going to say we're gonna make a buffer um it's going to be make me a slice of bites what's going on a slice of bites I'm going to say 1024 in this case just make it big enough and I'm gonna say the by three red error is going to be WS read into the buffet we just created and if that is an error you can choose what you do with this Arrow actually you could say we're gonna just print it out for example print a Ln read error and then just dump in the adult heat and you could say continue right because you need to understand that if you return here or break or whatever you're gonna drop the connection and it's lost so you need to you need to make this decision for yourself can the client make some kind of a malformed uh uh message or something or not right if he can then you could just continue maybe you made a mistake or something um which is unlikely but hey or you just drop the connection it's it's it's your it's your call it's your shot you're the Bob Ross of your painting we are just going to continue and you could also say something like if the Adolf no SBC yeah if at is going to be equal to IO and the file not close pipe and of file then we're gonna break here because if you have an end of files basically means that the connection on the other side has just closed itself and if that's the case it's going to be an end of file because this is the end of the file right and now we can just break it so we don't have any crazy Loops going on so what we're going to say then is going to be the message is going to be basically the perfect and the bytes we actually read so we don't actually print out the whole buffer that could be that they just sent three bytes so we are only gonna assigned three bytes to the message and I'm going to say print Alan a string representation because we are humans of this message and then what we also could do is basically in this case it's not going to be a chat we're going to make a chat real quick but first I want to show you this we can just reply with something like WS right and we need to write bytes so we're going to say thank uh thank you for the message that's what we're gonna write back getting a cold so what you're going to do here is you're going to say s or server is going to be a new server then we're going to say HTTP handle and we need to specify an endpoint where they can connect to so we're going to say slash WS this is basically default stuff but you're going to see for our feet we're gonna make something else and now we're going to say websocket um I think it's handled handlers and that's going to be server dot handle WS perfectly fine and I'm going to say HTTP listen and surf listen and let me type it out actually instead of pressing these typins suggestions listen and serve to the Holy Grail of ports which is 3000 let me know what your Holy Grail of Port is um back in the days I was using this one one three three seven hey this one remember hey all right so that's cool so all set up so we're going to test this real quick and then we're gonna make um or chat real quick yeah let's delete this uh go run main.go boom then I have this um look at this how to zoom the dev tools right I was actually recording this video already but it was so small so now I figured out how to um Zoom to devtools for you so uh but the first thing we're going to do is we're going to say let's suck it and that's going to be uh I already have it here right because I was testing this but very small and I thought man I need to zoom that in because otherwise the blind homies cannot see this so we're going to say you just do a new websocket and of course if you're using a libraries or Frameworks they are going to do it in another way but this is just plain vanilla JavaScript so you hey if you know this you can you can do if you can do any you can do anything right you're going to say new uh web socket and we need to specify uh our URL is going to be WS slash localhost 3000 uh uh and you can see I already did it because but hey with the smaller font so I need to redo it yes so we're gonna connect it to this endpoint we just created in our servers we're going to press enter and then we're gonna say we're gonna make some kind of a callback function for uh when there is a message it's going to be all message I think all message and that's going to be I think events and I'm gonna do this console log received from the servers this this and then event data we're gonna register that and then we're gonna say socket sent uh hello from client what's going on here with the help client like this boom and you see we send it to the server and the server responded with Reserve received from the server thank you for the message and you could see heat that there is a new incoming connection from the Chrome new def tab page whatever uh and then hello from the client and so that's work so next thing we're going to do multiple connections broadcast uh I make a simple chat um yeah cool it's going to be super easy guys hey like I said I'm the Builder of things I make things simple actually it won the Nobel Prize for simplification all right uh so the first thing we're gonna do is we're gonna make a simple function and that's going to be funk s servers brought broadcast uh um B is going to be a bunch of bytes just like that and we're gonna say for con for WS in a Range or connections right you see that's why we need a map and then we're going to say go we could do a Go Frankie we're going to say WS you could actually do um if add the blue column colon equal is Ws write this byte and then the ad is not nil classic goldling Shenanigans we're gonna um yeah returning doesn't doesn't make sense right you're going to say print online so we're going to just log this toward grafana or something hey I don't know you tell me the errors right you're going to say right errors boom boom boom no space heat WS right that's going on in it um does it actually return damn I see what's going on is okay no problem it's gonna return how much Buys have you written but hey okay and we need to say hi WS is going to be pointed to a web um websocket.com put in the wsh and that's fine all right so this broadcast very simple we're gonna range over all the connections and I'm gonna boom we're gonna just send uh the bytes to all of them right so for a chat we're gonna do this uh this is our message we have and each time somebody sends a message we're gonna let everybody know that there is a new message so we're going to say broadcast actually I was thinking maybe we should make a go go broadcast heat but hey we already did go Heat uh broadcast the message that's the only thing we're gonna do it's it's that easy and I'm actually thinking should we make is this a go function and then delete it each anyway it doesn't matter right so yeah let's go run boom so what we're gonna do here real quick is first of all we need to do redo all this stuff let me open up a new thingy here inspect boom console like this so I'm going to connect heat and we already typed this out so we're going to say we're going to connect each and then you're also going to connect heat boom right so you see we have two two incoming two Connections in our map we have the Chrome new tab page and then we have this Google thingy which is the Google thing Google page right so what we're going to do first is we're going to register our own message we're going to say receive from the server we press enter and we do the same thing here uh that's nice that that there is a history across all these steps it's amazing boom we're gonna do that and then we're gonna basically say here for example socket sent um hello uh ASL from who's remembering this ASL it was back in my day throughout all the chats and then um the first thing we say hey what's your ESL it's a long time ago I don't think a lot of you guys will understand what I'm talking about but ASL stands for H sex and language or something yes so we're gonna send this boom so yeah so see so we basically sent this message actually to make it even better um hello ASL my name is Bob like that boom you see and then we get it heat and then we're going to actually say here sockets sent hi my name is Lucy and I love to meet you like this for example boom and then you see they're chatting you know what I mean and you can open up as much uh tabs as you want and everybody can chat and yada yada simple chatting Google link websockets easy peasy of course there are a lot of things you can do you can subscribe to different topics and and there are a lot of things you can do but this is the basics and if you notice you can hey the sky's the limit okay so the next thing we're going to do is make some kind of a feat because that's a very important aspect in websockets where the browser can subscribe to for example a feet of orders for example an order book where you just subscribe to the order book and it will live in real time give you all the updates of the audiobook so that we're gonna make it's very simple uh we're gonna say we're going to place this thing let's do it heat we're going to say thank S servers handle WS order book or something it's going to be let me yoink everything inside this and paste help me and paste in here uh so we're going to say um let's showing this also we're going to say new incoming connection from clients uh to order book feed something like that uh we're gonna say here four let's make a format actually yeah you're gonna say fmt uh payload or something it's gonna be uh s print F I'm gonna say order book data and then we're going to say percentage D and we're going to do maybe a new line do we need that probably we do is it not quite sure we will see yeah we need yes let's do a new line I'm thinking so we're going to say time we're gonna make something up right this is basically garbage we can say time now so we have some some different thing Unix no no is that a thing yes because if we if we keep sending the same message we we will see this this incrementation in the dev tools right so one two three we don't need that we just want to have new messages all the time so now we're gonna just say WS right and we're gonna write um some bytes and I'm gonna say what's going on here with these big fonts payload right and then we're gonna save time to make and not overload we're gonna say time sleep time seconds let's do two seconds or something right um so what is this what's going on so this handle WS order book is basically a websocket subscription feed where people can subscribe to and everybody that's subscribing will receive um these order book data from us in this case it's just a simple payload uh we basically did ourselves but it could be something from your database and and all that that stuff you know what I mean this is just some example so how we're gonna do this is very simple we're gonna make uh we're gonna say HTTP handle uh and then you can give this whatever you want you could say order book uh feed or something right and I'm going to say web socket Handler servers uh handle WS audiobook you're done right then we're gonna reboot this thing go run main.go just like that and then I'm going to open up this boys or girl how can I clear this here so I'm going to connect real uh fast so we're going to connect it no we're not we are wrong we are wrong we are going to connect to the order book feeds right like this but then we need to on message boom you see and now we are getting actually um live data from our order book and the cool stuff is that uh we can we can actually make do it the same same way here right we could say connect to the order book feet yes and then do this thing again on message right and we're receiving heat and we are also receiving Heat and we are receiving heat so everybody that's connected can receive uh the the live feed of the audiobook that's what it is that's basically uh web sockets in a nutshell uh in golang I think if you understand how it works how you can set up things how you can structure things like I showed you you can basically um make anything you want in this if you like the video I am providing to you please consider subscribing to my channel give me a thumbs up leave some questions in the comments very important is that you also jump into my Discord Community where I'm turning developers into high value software engineers and for the people that are really want to level up uh even more exclusive I have content on my patreon page check that out thanks for watching and I'm looking forward to see you in one of my live streams or future videos cheers
Info
Channel: Anthony GG
Views: 45,293
Rating: undefined out of 5
Keywords: websockets, websocket, websockets tutorial, golang websockets, go websocket chat, golang chat server, golang, websocket server, golang tutorial for beginners, learn golang, golang tutorial, golang for beginners, websocket example, go programming language, go programming, golang beginner tutorial, golang programming, golang websocket server, golang websocket example, go language, go tutorial, go lang, go programming tutorial, golang tutorials
Id: JuUAEYLkGbM
Channel Id: undefined
Length: 20min 45sec (1245 seconds)
Published: Fri Dec 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.