Mastering WebSockets With Go - An in-depth tutorial
Vložit
- čas přidán 1. 06. 2024
- WebSockets is one of the oldest ways to communicate in a bi-directional way and is widely used today. It is supported by most browsers and is relatively easy to use. WebSockets are really useful when building real time apis.
In this tutorial, we will cover what WebSockets are and how they work, how to use them in Go to communicate between servers and clients. We will also explore some regular pitfalls that I've seen in WebSocket APIs, and how to solve them.
During the tutorial, we will be building a chat application where you can enter different chat rooms. The WebSocket server will be built using Go, and the client connecting in vanilla JavaScript. The patterns we learn and apply could easily be adapted when connecting using a Websocket Client written in Go, Java, React, or any other language.
We will learn about Authentication, Heartbeats using Ping & Pong, Cross Origin and a number of other useful WebSocket related things.
If you prefer a written format of the video instead you can visit my blog
programmingpercy.tech/blog/ma...
00:00 Introduction
03:04 What are WebSockets
05:46 Project setup
18:46 Connecting Websocket
28:14 Adding Clients
33:46 Reading & Writing Messages
53:00 Events
01:12:24 HeartBeating
01:22:27 Jumbo Frames
01:24:36 Cross Origin
01:27:37 Authentication
01:55:10 TLS
02:02:18 Finalizing
Links mentioned in the article
Full Code - github.com/percybolmer/websoc...
Cross Site Request Forgery - owasp.org/www-community/attac...
Beach by MBB | / mbbofficial
Music promoted by www.free-stock-music.com
Creative Commons Attribution-ShareAlike 3.0 Unported
creativecommons.org/licenses/... - Věda a technologie
Great time to watch the tutorial. Gorilla toolkit was archived for the past 8 months. But 2 days ago it became active again after new maintainers took over. No better time to watch this.
thank you for this tutorial! I really appreciate that you invested your time to make this video.
I would like to thank you for this amazing, detailed tutorial. This is exactly what I'm looking for. You're a lifesaver.
Thank you for letting me know you enjoyed it!
Thanks for the step-by-step guide!
extremely well done and complete example, great job sir!
Thank you sir, glad you enjoyed
@@initialized glad you mentioned I my mic, I know it sucks!
I am so happy to say that I have bought a Mic & Lightning !
Just trying to fix a virtual green screen or physical one!
Thanks for mentioning it, so that I can improve
Amazing tutorial. Thank you
This tutorial is awesome and explained so well - thanks
Big thanks for the kind words
This is a great tutorial, thanks you!
Wow ,Thank you so much for sharing the amazing content, liked 👍 and subscribed
Very clear. Very helpful.
Thank you for the kind words, thrilled to hear you enjoyed
Hi I`m beginner in websockets and your video helped me to better undestand how to use gorilla building simple messengers.Your video is awesome!!
Thank you Yegor! Glad i could help
best golang ws chat tutorial, thanks a lot !!
Thank you for the kind words
Awesome work bro
Great video and topic
many thanks
Simply Fantastic, helps me a lot.i came here reading your medium post, Thanks a ton brother for the amazing content.
I have a question though...
I am building an Android game using kotlin in the front end and i am planning to choose Go at the backend and obviously I wil be using websockets, this video helped !!
Great video!
Micro from DDD video was definetly better btw
Great tutorial! Looking forward your other tutorials!
Too bad that your mic isn’t great. I think your content is so good that I would gladly swish 30-50 kr towards a new microphone. :)
Thanks bro!, yau are the best!
Nah, you're the best!
@@programmingpercy BROOO two weeks developing for my company, and your tutorial was my handbook, you help us a lot with the structure of our system
@@programmingpercy I'm a junior developer well, i'm still studing this engineering, but you help me a lot!!, i made a CRUD with your tutorial, you are the best
Awesome tutorial! I have one question, your pong/pong handling, from what I can tell its not doing anything if the client stops sending pongs back.
Just what i was looking for, Thanks! is there a Part II? connecting to an external data storage (redis)?...
Hey!
Hmm no part 2 planned.
I do have an article on using Redis for PubSub, maybe I can update it Abit and Record that video. Only exists as text atm.
programmingpercy.tech/blog/redis-as-a-pub-sub-engine-in-go/
@@programmingpercy thanks 🙏 reading it right now
When we say that Gorilla 'Connections only support one concurrent reader and one concurrent writer', does this mean that for *each* client connection we can only have one goroutine reading messages and one gorountine writing messages via a channel? Obviously, if we have multiple clients, there will be an equivalent number of reading and writing goroutines - but I assume this is not what the restriction is referring to?
Great question!
To clarify, the gorilla connection is not safe to have concurrent writers.
This is only true for writing, data into the connection.
They have an example in their source code with a lot of comments that explains this if you'd like a more in depth explanation.
So in short, we simply cannot spawn multiple goroutines that all tries to write on the same time. It might work at first glance, but there is a race risk going on.
That's why we instead send messages on a Channel, and then send those one at a time. To prevent concurrent writes.
Thanks for this, learnt a lot! I just have one question, how would you structure rooms? I dont want to broadcast mesaages to every client, I just want to send message to clients in a group. One thing I was thinking to do something like `/ws/{room_id}` and then clients will automatically be in a group thenI dont have to do any special in my code to handle rooms. Is this a correct assumption? EDIT- Okay, I just tested this,. it doesn't work this way xD
This is absolutely amazing, thanks nigga !!!
Firstly Thanks and Nice explained !!
BTW I have one question, how do we handle the web socket connections in Distributed system (like multiple instance are running for the same service)
Great question.
At the point where you start running multiple instances, it is common to include Redis or RabbitMQ to allow distributed messages for the websockets.
You would then listen on the PubSub schema used and push messages on RabbitMQ/Redis, then read from those topics and push onto the Websockets.
@@programmingpercy thanks👍
Gorialla websocket is limiting to only 1 concurrent connection at a time, is this limitation only for Gorilla websocet or its same for other Go web libraries as well ?
I know that the x/net/websocket supports multiple, but that package is also deprecated and frozen. That package has trouble with defragmented frames tho.
I can't speak for gobws since I haven't used them.
Let me make this clear, it's only limited to one concurrent WRITE, and this shouldn't pose an issue if implemented correctly, it scales pretty good. So I don't think many people have had an issue with that.
Hi, I want to use go to connect to the asterisk server.
I want to ask in what moment data insert to c.egress in ReadMessages 1:10:00?
sound is sick but good presentation
I am making sure to fix the sound for future videos! Thanks!
old stuff 🎉
Gorilla? It is deprecated.
Yeah! Video was made before that!
I have in mind to create a new one but I haven't decided on which OS solution I like best amongst the others.
@@programmingpercy It's archived, but so what. It hasn't changed for many years, and it works. I wouldn't worry about using it.
It's just if there is any RFCs that gets out that might be scary.
@@programmingpercy cant wait, this video got me to understand websockets in go
It is active again
Gorilla is dead.
Actually that's not true
www.reddit.com/r/golang/comments/1528e25/gorilla_web_toolkit_new_maintainers_announcement/?
It's alive again
Thanks brother, have a good day
didn't understand anything even after spending 3 days to watching this tutorial
No way, man! It's not even that hard. You mustn't have coded along with the video. This video is a gem. I spend like a day watching this tutorial for my chat application and I guess I've implemented the websocket part of the application effectively. I implemented something similar after watching the video, an event based websocket system, but with slight differences. Instead of using OTP, I'm using JWT for authorization, MongoDB for storing user details and messages (I'm not using any message queues or stuff for now as it might add unwanted complexity to the program), and gin frame work to stick everything together. If you are not a video person, just read his blog. It's much easier to follow along. Also, try to understand the logic behind websockets and how to utilise it. Then implement it in your own way, which you will find is much easier to maintain the code base. Happy coding!
@@codeunited5905 no I was coding alongside the video but didn't understand anything. It's my personal opinion, no hate to you or your channel.
No worries man, no offense taken.
It's really hard to create content that fits everybodies needs.
One might find a tutorial progressing too fast whilst others find it way to slow.
But if you don't mind me asking, what in particular made it hard for you to follow?
Is it to fast? Would you like deeper explaining on what's going on?
It would be great to know so I can improve future videos
@@programmingpercy I'm a beginner in golang, so most of the concepts were confusing for me
Bruh
The video shows at the beginning that he expects viewers to be ‘somewhat familiar with go’
You need to build your basics before you could reach the skill level for this video
No hate is good, you better come back and rewatch after you progress
Man what do I use gorilla mux or socket io for golang ?
I like gorilla much, there was a bit trouble when the maintainer decided to archive it.
But since then it has gotten a group of new maintainers and is in a good place.
I'd stick with Gorilla,