Skip to main content

Detailed Understanding of What a Web Socket Really Is?

A Web Socket is, in its simplest explanation, a connection between two processes running in two different (computer) systems.

Detailed Explanation:

There are a few terms that you should know about to fully understand Web Sockets.

Note: A computer system is everything from your mobile device, to your laptop/PC to a proper Web Server.

IP Address: 

IP Address is a unique Identification of a computer system for the Internet.

You log onto Instagram.
There is an Instagram server situated somewhere far away.
Any page you open, anything you click sends a request to that server.
Within that request, obviously, some data is requested.
Along with that, the request should also have a FROM property, right? How can anyone know who to respond to unless they know who has asked the question?!
That required FROM tag, is the IP Address of your device.

Everywhere you go on the Inernet, it's basically this IP Adress going in places and getting you all your results.

IP Adress somewhat looks like this:

There's no need to go into more detail than this.


Everything working in your device is a process.

  • You open an app in your phone, that's a process.
  • You start a Chrome tab in your PC/Laptop, that's a process. You're playing a game, that's a process. 
  • You're working on MS Office, that's a process.

Basically, every interaction in your system is taken as part of a bigger process by your system.
Now that you've understood the meaning of a process, we can move on.


For every process that wants to access some data by sending a request (as discussed in the IP Address section), the computer system allots a port number to it.

There are multiple requests being sent from a computer system.
Take your phone for instance.
Your phone is sending requests from the Whatsapp process, Instagram Process, LinkedIn, Gmail, and so many such apps that provide you notifications throughout the day.
So much of data must be flowing into your device from different servers.
How does your device identify which process' data is this?
By looking at the Port Number specified in the response.

The port number is also sent in the request by the device, and by standard, every response sends back that same port number as its sub-destination. Remember, the main destination of any request/response is the IP Address.

Okay! That's it. Now you're all set.

Web Socket.

Web Socket is an established connection between 2 different systems. Both the computer systems temporarily save the other system's IP Address and Port Number in their processes and send data to each other.
Thus fundamentally, a Socket is nothing but an IP Address and a Port Number.

Web Socket
Image result for client server model
A typical client-server model.
Credits: TutorialsPoint
Here's what is happening.
System A's process 'x' wants to send some data.
System allots the process a port number.
The IP Address and Port Number are sent along with the process' data.
System B receives the message.
It unlocks and finds the message is from a system with some IP Address, and some port number.
Now, it saves this temporarily. If it has to send some data back, or even after some time, it will pick up this combination and ask the system to send it to this address.

Did you notice how this made the client-server model look so redundant?
System A/B can be a Web Server and still send data to the client device without the client even asking for anything!
For example, the chat messages that arrive in your device the very second they are sent by another user.

There are no requests/responses in Web Sockets, just data.
Because a connection is established that virtually joins the 2 systems.
This connection is called TCP/IP. You can learn more about it here: TCP Wiki.

If you have any questions, feel free to ask them. 


Popular posts from this blog

Namaste JavaScript Quick Notes

Note:  Akshay Saini's Namaste JavaScript is probably the best course for JavaScript developers out there. These are my personal notes that I made while watching the course; they serve more of as an online quick reference for my understanding and revision, and I hope it benefits anyone reading it too! Everything in JS happens inside an Execution Context. Before a JS code is run, memory is allocated and variables are set as undefined   , and functions are set as their exact code in the scope within the Execution Context. The global execution context hosts all the global variables and function definitions. An Execution Context has 2 components: Memory, that stores variables and functions; and Code, that reads and executes the code. Call Stack maintains the order of execution contexts. Since JS is single threaded and asynchronous, at one point of time, only one function is executed which is at the top of the call stack. For each function, an execution context is created before executi

i3wm essentials - I (Brightness)

So you have started using i3 and somehow managed to open your browser and almost resumed your normal work.  But wait, the brightness is too much isn't it? Or is it too low? The mousepad used to work fine, but now all of a sudden tapping does not equal click?!  Don't worry.  This blog series will tell you all about the essential setup commands and common shortcuts that I use to navigate my work in i3, and how you can too. Changing the brightness So you just started i3 and you just can't take this brightness setting. You go for your function keys, and damn! They aren't working. Quick fix: Run the following command if you need to change the brightness ASAP. xrandr -q | grep ' connected' | head -n 1 | cut -d ' ' -f1 This will give an ouput that's the name of your monitor.  Use that monitor name here and change the values of brightness to suit your needs. xrandr --output <monitor-name> --brightness 0.7 Now that your eyes are comfortable, let me show

"Hey Google" get me a new T-shirt

Everyone loves Google for its amazing technology and creative workspaces! Guess what? Google loves its developers as much as the world loves it too! And yeah, you don't need to be an amazing programmer to be a developer in Google's community. All you need to do is to spare 30 minutes, just once, and maybe have some creativity! That's it! Oh, and you should be really checking your mails periodically, although if you don't currently have this habit, your excitement would develop that for you. What do we want? So you arrived here to know about getting a T-shirt. Would you also like having a Google Home ? Yup, that is also something you could get through this. And of course, as I mentioned earlier, an entry to Google's Developers Community Program! There're a lot of perks for it but let's first talk business. What do we have to do? In a nutshell: Make an Action for Google Assistant .  But what's an Action ? Action is a feature, or a sub-applicat