Web Sockets and Difference between Web Sockets and HTTP

Web Sockets and Difference between Web Sockets and HTTP

Web Socket?

A Web Socket is a communication protocol that provides a full duplex , communication channel over a single, long lived connection between a client and server. That goes over your head right , let me break down it in simple terms. Suppose you have a server and client and now if you want continuous connection between them then you use web sockets. If the connection is established between server and client they can send data whenever required.

Server does not have to wait for clients request to push back any data and the client does not have to make new connection to send any data to the server.

Web Sockets are very useful for real time applications like trading or chatting app where live data is required. So if we need to have data which is frequently changing then we need web sockets as it is a better example of how data can be collected without sending continuous request.

Difference between HTTP and Web Socket

HTTP is a one-way, stateless protocol where a client sends a request to the server, which then establishes a TCP connection to respond accordingly. Once the response is sent, the connection is typically closed. If the client needs to interact further, it must initiate another request, leading to the establishment of a new TCP connection.

In contrast, WebSockets offer bidirectional, stateful communication. They enable the establishment of a continuous connection between a client and a server, allowing data to be transmitted in both directions without waiting for the traditional request-response cycle. This continuous connection is particularly advantageous for real-time applications where instant data updates are crucial, as it eliminates the need to repeatedly establish connections for each interaction.

Working

Firstly , a client sends a request to the server which is basically a http request (a http handshake request with upgrade header) and server sends back with 101 switching protocol response (handshaking completed) and a web socket is established between a server and a client.