DEV Community

Cover image for HTTP vs WebSockets
Rudra Pratap
Rudra Pratap

Posted on • Updated on

HTTP vs WebSockets

HTTP

Whenever you type any URL in the browser, say https://google.com and hit enter, a request goes to the DNS server in order to get the IP address behind the domain name.
When the client gets the IP address, it establishes a TCP connection with the server on the cloud.

Image description

HTTP is based on client-server architecture, ie client requests for some data from the server, the server responds and then the connection is terminated.

Image description

HTTP is stateless. ie every time client wants to send data to server, it has to explicitly specify all the headers like cookie, authorization etc headers, so that the server can recognize it.
Benefit of being stateless is that the request by the client can be served by any server in our backend as there is no state established among the server and the client. This enables us to easily scale the system horizontally.

HTTP connections start with http://yoursite.com.

HTTP is an application layer protocol built on the top of TCP/IP.

Websockets

The Websocket protocol is stateful. It also used TCP under the hood.
Here, the connection is not terminated after request-response process, but it exist until one of the party has not closed the connection explicitly.

Image description

Websockets have low-latency, that is why they are used for realtime communication, chat applications, gaming etc.

Websocket connections start with ws://yoursite.com

Drawback: If a connection is established, we can't transfer the next request to other server because if we do so the state will be lost. Hence it creates problems while scaling.

Top comments (0)