# HTTP v/s Web Sockets

Ok so was trying to build a project on creating a real-time chat application and came across this term of web sockets. Never heard of it and was always using http protocols to handle the client server model. Apparently for chatting ,video applications web sockets are the best way to go.

Let us understand in simple words the difference between web sockets and Http protocols.


# How Http Works

Http is a single way communication between the client and the server. What this means is that lets say we have a client which is google chrome and a server by the name of dhruv. The chrome is asking for get requests to the dhruv and the dhruv server where the endpoint hits provides the neccassary content to the chrome client. Similarly the chrome can also send contents to the dhruv server in the form of POST requests. However in case of HTTP once the task is over the connection established gets over. When the chrome again tries to ask for contents a new connection is established with the server. Every time the session gets terminated once the task is completed.

Hence HTTP is a stateless protocol which means that stateless Protocols do not need the server to save the state of a process.

HTTP protocols have the advantage of horizontal scalability 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648752684157/3ZOhyGgB4.png)

# Web Sockets

However web sockets work a little differently. Again here there is a client and a server but now the client server connection is a keep alive connection i.e the connection once established could remain connected for a long time till the client and the server are sending and receiving the data with one another 

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.

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. 


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648753139022/Vp0B8FL6k.png)


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