Mastering WebSockets With Go - An in-depth tutorial

Sdílet
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

Komentáře • 68

  • @IvanRandomDude
    @IvanRandomDude Před 10 měsíci +12

    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.

  • @paca3107
    @paca3107 Před 6 měsíci +1

    thank you for this tutorial! I really appreciate that you invested your time to make this video.

  • @MonsterGames71
    @MonsterGames71 Před 9 měsíci +10

    I would like to thank you for this amazing, detailed tutorial. This is exactly what I'm looking for. You're a lifesaver.

  • @jxiongdevdiary
    @jxiongdevdiary Před 6 měsíci

    Thanks for the step-by-step guide!

  • @YandryPozo
    @YandryPozo Před rokem +10

    extremely well done and complete example, great job sir!

    • @programmingpercy
      @programmingpercy  Před rokem +4

      Thank you sir, glad you enjoyed

    • @percybolmer4343
      @percybolmer4343 Před rokem +1

      @@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

  • @daicaphong84
    @daicaphong84 Před 10 měsíci +2

    Amazing tutorial. Thank you

  • @jaypatel1393
    @jaypatel1393 Před rokem +2

    This tutorial is awesome and explained so well - thanks

  • @user-ip1wg4sg3w
    @user-ip1wg4sg3w Před 3 měsíci +1

    This is a great tutorial, thanks you!

  • @mahendranath2504
    @mahendranath2504 Před 5 měsíci +1

    Wow ,Thank you so much for sharing the amazing content, liked 👍 and subscribed

  • @evanfang6573
    @evanfang6573 Před rokem +3

    Very clear. Very helpful.

    • @programmingpercy
      @programmingpercy  Před rokem

      Thank you for the kind words, thrilled to hear you enjoyed

  • @yegor3793
    @yegor3793 Před rokem +2

    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!!

  • @dhaw
    @dhaw Před 5 měsíci +3

    best golang ws chat tutorial, thanks a lot !!

  • @pindi-zx4cs
    @pindi-zx4cs Před 4 měsíci +1

    Awesome work bro

  • @nelsonbalmaceda7046
    @nelsonbalmaceda7046 Před rokem +1

    Great video and topic

  • @fouad-hachour9968
    @fouad-hachour9968 Před rokem +1

    many thanks

  • @bjugdbjk
    @bjugdbjk Před rokem +2

    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 !!

  • @Flascoo
    @Flascoo Před rokem +1

    Great video!
    Micro from DDD video was definetly better btw

  • @SvartAdamSolander
    @SvartAdamSolander Před rokem +1

    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. :)

  • @mr.mendez7094
    @mr.mendez7094 Před 11 měsíci +2

    Thanks bro!, yau are the best!

    • @programmingpercy
      @programmingpercy  Před 11 měsíci

      Nah, you're the best!

    • @mr.mendez7094
      @mr.mendez7094 Před 10 měsíci

      ​@@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

    • @mr.mendez7094
      @mr.mendez7094 Před 10 měsíci

      ​@@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

  • @skl9942
    @skl9942 Před 3 měsíci

    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.

  • @notnull01
    @notnull01 Před rokem +1

    Just what i was looking for, Thanks! is there a Part II? connecting to an external data storage (redis)?...

    • @programmingpercy
      @programmingpercy  Před rokem +1

      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/

    • @notnull01
      @notnull01 Před rokem

      @@programmingpercy thanks 🙏 reading it right now

  • @dfdkjr8719
    @dfdkjr8719 Před 9 měsíci +1

    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?

    • @programmingpercy
      @programmingpercy  Před 9 měsíci

      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.

  • @TheRj725
    @TheRj725 Před 9 měsíci

    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

  • @mikealejandro3938
    @mikealejandro3938 Před 5 měsíci +3

    This is absolutely amazing, thanks nigga !!!

  • @shubhamverma572
    @shubhamverma572 Před 10 měsíci +1

    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)

    • @programmingpercy
      @programmingpercy  Před 10 měsíci

      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.

    • @shubhamverma572
      @shubhamverma572 Před 10 měsíci

      @@programmingpercy thanks👍

  • @bjugdbjk
    @bjugdbjk Před rokem +1

    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 ?

    • @programmingpercy
      @programmingpercy  Před rokem

      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.

    • @programmingpercy
      @programmingpercy  Před rokem +2

      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.

  • @9ezi435
    @9ezi435 Před rokem

    Hi, I want to use go to connect to the asterisk server.

  • @boriskrasnov1672
    @boriskrasnov1672 Před 6 měsíci

    I want to ask in what moment data insert to c.egress in ReadMessages 1:10:00?

  • @hiyoutube6769
    @hiyoutube6769 Před rokem

    sound is sick but good presentation

    • @programmingpercy
      @programmingpercy  Před rokem +1

      I am making sure to fix the sound for future videos! Thanks!

  • @catalinim4227
    @catalinim4227 Před 6 měsíci

    old stuff 🎉

  • @micha9138
    @micha9138 Před rokem +1

    Gorilla? It is deprecated.

    • @programmingpercy
      @programmingpercy  Před rokem +1

      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.

    • @michelemendel
      @michelemendel Před rokem +1

      @@programmingpercy It's archived, but so what. It hasn't changed for many years, and it works. I wouldn't worry about using it.

    • @programmingpercy
      @programmingpercy  Před rokem

      It's just if there is any RFCs that gets out that might be scary.

    • @notnull01
      @notnull01 Před rokem +1

      @@programmingpercy cant wait, this video got me to understand websockets in go

    • @IvanRandomDude
      @IvanRandomDude Před 10 měsíci +3

      It is active again

  • @m_205
    @m_205 Před 10 měsíci

    Gorilla is dead.

    • @programmingpercy
      @programmingpercy  Před 10 měsíci +1

      Actually that's not true
      www.reddit.com/r/golang/comments/1528e25/gorilla_web_toolkit_new_maintainers_announcement/?
      It's alive again

    • @m_205
      @m_205 Před 10 měsíci +1

      Thanks brother, have a good day

  • @sahildhingraa
    @sahildhingraa Před 10 měsíci

    didn't understand anything even after spending 3 days to watching this tutorial

    • @codeunited5905
      @codeunited5905 Před 10 měsíci

      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!

    • @sahildhingraa
      @sahildhingraa Před 10 měsíci +1

      @@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.

    • @programmingpercy
      @programmingpercy  Před 10 měsíci +5

      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

    • @sahildhingraa
      @sahildhingraa Před 10 měsíci

      @@programmingpercy I'm a beginner in golang, so most of the concepts were confusing for me

    • @varunzid123
      @varunzid123 Před 3 měsíci +2

      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

  • @SwarajSingh-xs6dy
    @SwarajSingh-xs6dy Před 7 měsíci

    Man what do I use gorilla mux or socket io for golang ?

    • @programmingpercy
      @programmingpercy  Před 7 měsíci

      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,