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: 192.168.40.3

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

Process:

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.

Port:

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.
Why?
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. 











Comments

Popular posts from this blog

How Kafka replaced Zookeeper with the (K)Raft algorithm?

Back in 2021, when I first came across Kafka, I remember the DevOps engineer in my team using terms like Zookeeper, broker configs, etc on our team standup calls. I remember not caring about those terms, and simply focusing on learning about the producer, partitions, topics and consumer groups, and how they could be used in the product my team was developing. While platforms like Kafka were built to abstract certain aspects of distributed computing (replication, consistency) while storing & processing logs, it's a pity how so many of us miss out on knowing the amazing engineering that went behind to build the different parts of a platform such as this one. 4 years later, I'm hungry enough to reverse engineer one of my favourite distributed platforms - Kafka! What did the Zookeeper do? To quote the 1st  Kafka paper from 2011, Kafka uses Zookeeper for the following tasks:  (1) detecting the addition and the removal of brokers and consumers,  (2) triggering a rebalance ...

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

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