In this blog I will explore the differences between HTTP and Websockets, as well as explain when it is best to use Websocket over HTTP. I will do my best to keep this short and sweet ;)
HTTP is a computer protocol through which the client sends a request to and receives a response from the server. Each request results in a high cost and performance loss. Thanks to the ability to send two-way messages over TCP connection with Websocket technology, we can get rid of the high cost and have the opportunity to implement real-time applications perfectly.
Indeed, Websocket technology is a protocol that can send two-way messages over a continuously open TCP connection. Through this open TCP protocol, it is possible for the parties to send each other message and there is no obligation to respond to the sent messages as there is in the HTTP protocol.
If you examine the HTTP / REST Request functionality from the above scheme, you will see that there is a response to each request. In the HTTP protocol, it is mandatory to return a response whether or not a request is returned.
If you examine the Websocket schema, the whole process is running over an open TCP connection. After the handshake process, once the acknowledgement is done, the bi-directional message process starts and goes until the connection ends.
In short, Websocket technology may be preferable to HTTP because of its:
- Bi-directional protocol - Unlike HTTP protocol, which is uni-directional, the request can be initiated by both the client and the server.
- The parties can send messages without expecting any response from the receiver.
- Full-duplex communication - The parties can send messages at the same time.
- Single TCP connection - The parties communicate over that same TCP connection throughout the lifecycle of Websocket connection.