What are Web Sockets? How is it different from HTTP?
Tags: http, websockets
Published: 2021-08-26 04:59:36

Watch the full YouTube video [here](https://youtu.be/xTR5OflgwgU)

HTTP and Web Sockets are two protocols that we regularly use as a developer. In this blog, let's understand and compare both.

## What is HTTP protocol?
For beginners, HTTP stands for HyperText Transfer Protocol. It is a protocol that allows the fetching of resources, such as HTML documents. It is the foundation of any data exchange on the Web, and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser. ‌
[More on HTTP here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview)

## How HTTP works?
So when you want to visit google.com and type the URL in the address bar, the HTTP first establishes a TCP Connection to Google's Server.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmby3lccogrpjkl5142y.png">
  <figcaption>Figure showing the steps involved in making the TCP Connection</figcaption>
</figure>

TCP is the fourth layer protocol as per the Open Systems Interconnection Model (OSI Model). OSI Model is how we depict the network model.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03np6arnk4jh7baioai5.png">
  <figcaption>7 Layers of the OSI Model</figcaption>
</figure>

TCP is a connection-oriented and reliable full-duplex protocol supporting a pair of byte streams, one for each direction. Before exchanging data, a TCP connection must be established. The TCP connection is established by performing TCP Handshake. After the handshake is done, the client (i.e. your computer) sends a GET request inside a TCP Packet to Google's Server asking the HTML Document and the required resources to show the Google Homepage to you.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnpnmge1q1ys909ew4t7.png)
 
HTTP is a single way communication. The client requests and the server responds or vice versa, whereas TCP communication is two ways. Here the client and the server can speak simultaneously. That is it. That's all that HTTP does.

## HTTP is a stateless protocol
As we move up the OSI Model stack, you might wonder why HTTP does not have a two-way response like TCP. You might have just found out the reason. Yes, HTTP is a stateless protocol that means an HTTP server need not keep track of any state information. So, at any time, a client can send any valid command. The server will not relate this command to any previous or future commands.

You might be wondering what good does this do to us. How is it beneficial to the client or the server? As HTTP is a stateless protocol, it always has to send request headers with HTTP requests. Because of this, the server architecture becomes highly scalable because many servers can respond to far many clients and share the load of requests between them.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqailw7gdm0yoji4h7f1.png)

## How do Web Sockets work?
However, Web Sockets works differently when compared to HTTP. Web Sockets works somewhat similar to how TCP works on the grounds of dual communication. Like HTTP, Web Sockets also work with the help of TCP. When the client first sends a Web Socket request, it sends an HTTP request and asks the server if the connection can be upgraded to Web Socket.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnhi00ohg19ark8ho2yj.png)
 
‌If the server accepts the request, then only the HTTP connection is upgraded to Web Socket. Now the dual channel is established between the client and the server.

## Difference between HTTP & Web Sockets
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjpv4o41xf9z15p8hhju.png)
 
Web Sockets are like a phone call. Once established, data can be shared either way. While in HTTP, it is more like sharing letters between each other, and when the server replies with its letter, the connection ends. To request it again, a new connection must be established before data can be shared or requested. Here HTTP is like a one-time transaction.

## Scalability Problem
While using the Web Sockets, if the client sends, e.g., 10,000 requests to the server at a particular point in time, the server may not process all the requests as Web Sockets are stateful in nature. HTTP requests are stateless, so their sent requests can be processed by multiple servers and sent back faster when compared to Web Sockets here.

## Web Sockets is a stateful connection
As mentioned earlier, Web Sockets are stateful in nature. They don't need to establish the connection before responding or requesting. So Web Sockets always has to maintain a state to determine the requests and responses stream. While this makes the mutual transfer of data seamless, sheer amounts of data cannot be transferred at a particular time. So while using web sockets, the server cannot be scaled horizontally. As a result, it can only process a certain amount of requests. While web sockets have the advantage of seamless transaction data, it comes at the cost of scalability.

This scalability problem can be partly addressed with the help of AWS API Gateway.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hphf8t9az66zt0y5auk.png">
  <figcaption>Comparison between web sockets and http requests </figcaption>
</figure> 

## Conclusion
Web Sockets and HTTP come with both advantages and disadvantages of their own. ‌
‌Web Sockets are mostly used when setting up a stream, video chat, and other messaging and constant communication tasks. While HTTP is used to manage the sheer amount of requests.

Hope you were finally able to understand the difference between the two. By now, you'll know abstract working and use cases of both HTTP and Web Sockets.