WebSockets (using Socket.io) Tutorial #1 - What Are WebSockets?

Knowledge Lighthouse
57 followers
1 Video View·Oct 22, 2022
Hey what's going on gang and welcome to your very first WebSockets tutorial.
All right so what are WebSockets? Well basically they're a way to communicate between a client,
the browser and a server and this communication is bi-directional and that basically means that
data can flow in both ways. So it can flow from the client to the server and also from the server
to the client. Now because these WebSockets are always open that allows for real-time data flow
in our application. So this is all really cool but what does it mean in a real world example?
Well I want you to imagine we have this kind of chat application which is hosted on this server
right here. So all these different clients right under here are different people around the world
in their browser and they all go to whatever.com to request this chat application in their browser.
So now they're all viewing it in their browser. Now say one person John over here he typed
something into this chat and click send then that message is going to instantaneously be updated in
all of these different clients around the world that are viewing that chat as well.
That's how chats work right? So this is WebSockets in action but what's going on? Well when we first
request the chat application okay in the browser we get that chat application back but also at the
same time what we're doing is we're opening up this WebSocket between the client and the server.
So now we have all of these different clients all of these different browsers around the world
which all have their own WebSocket connection to the server right and that means that data can flow
back and forth between it in real time because these are always open these WebSockets. So now
when John actually creates a message and click send what we're doing is sending that message
down this WebSocket to the server. The server receives it and then says hey this is a chat
message I know what to do with that I'm going to send it down all these other WebSockets which
connect into these clients down here to each individual client so that they can see that
updating message in their chat. So the message is getting sent up the WebSocket to the server
then the server sends it down each individual WebSocket to all the remaining clients
and that's why they see this message instantaneously. So I want you to take note here
this data transfer is occurring in real time and we're not using any kind of AJAX requests from
each of these different clients to request any new data from the server. It's all happening without
the client having to make any additional requests at all. So this stuff is really cool and there's
tons of different uses for WebSockets not just chat rooms and they include making
multiplayer browser games, collaborative code editing software, some kind of live text for sports
on news websites, online drawing canvases, real time to do apps etc etc. There's tons of different
uses for WebSockets. Now in this tutorial we are going to make a simple chat application that looks
something like this. So we have it open in two different browsers here we're connecting to the
same server in both instances and I want you to imagine that instead of these two browsers on my
computer this is two different computers in two different parts of the world because it works
exactly the same way. So they're both connecting to this server and they're getting this chat
application back in their window which is some kind of Mario chat. Now if you've watched enough
of my videos you know I'm a fan of Mario this is a recurring theme but anyway in this chat window
we have an input for a handle so that's your name so I'll just say Luigi and I'll do the other one
as well say Peach okay and we also have an input for the message so I can type a chat here so I'll
say hey Peach notice this when I'm typing here I'm getting this Luigi is typing a message over in the
other client so this is WebSockets in action pretty cool right and also when I click send
then we get the updated message right here to say Luigi has said hey Peach and also over here so
we're using WebSockets to send that data to the server the server then sends it down to this
client and shows it in real time and we've not made any kind of request from this client to the
server at all it's just open and listening through this WebSocket and receiving data. Likewise if I
type some kind of message in this one hey Luigi then we also see this Peach is typing a message
over here and click send same happens we get the message over here using WebSockets as well as over
here instantaneously really quick in real time okay so that's what we're going to create in this
set of tutorials now I want to show you a couple of tools I am going to be using the first one is
Atom a text editor completely free and completely cool so you can download that at atom.io also I'm
going to be using cmd or commander don't know how to pronounce it for my command line tool again
that is really free sorry that is really cool and it's free as well and now before you start this
series you are going to need node.js installed as well so you can head to nodejs.org to download that
just click on this button right here and that's going to download it for you walk through the
installation steps and then to check whether you have node installed on your computer just open
your command line tool this is commander which I showed you a minute ago and just type in node
if you've got it installed it's going to throw back the version number right there otherwise it
won't okay so make sure you have node installed and finally as always I'm going to provide you
with all of the course files for this series they can be found on my github repository right here
called WebSockets playlist I'm going to leave this link down below as well each lesson corresponds to
a different branch so just select the lesson that you want and you're going to get the code
for that okay and you can download it all here so there is your introduction to WebSockets
really hope you enjoyed the rest of this playlist and please guys if you do like these videos please
please please remember to hit the like button maybe even share them if you're feeling generous
and I'm going to see you in the very next video
