I have a question regarding these three data transmission protocols on the Internet. I know that TCP/IP sockets are a form of low-level communication that uses the transport layer to perform data transmission and handles it very well.
But the WebSockets and the HTTP Request , I understand that they are built on the application layer and from the examples that I have seen both are very similar in terms of their form.
I am right? Are WebSockets an enhancement to HTTP Request or are they the same thing? In what cases is it better to use WebSockets than an HTTP Request?
Simply put: TCP sockets are very low-level connections. You can see it as the physical connection between two computers. When you establish a TCP socket you can send and receive data synchronously or asynchronously, depending on the protocol you use from that moment on.
HTTP is a synchronous protocol that goes over TCP. An HTTP request is made over a TCP socket (and the response travels over the same socket).
Web Sockets is a protocol that goes over HTTP. A web socket connection is made through a TCP socket, using the HTTP protocol initially.
TCP/ IP sockets are what make client-server architecture possible, though they are not limited to that. They are actually involved in all kinds of communication as they are the mechanism for the delivery of data packets between computers.
So having the IP addresses and ports you can still choose which transport protocol you want to use. The transport protocol can be any of those belonging to the transport layer or layer 4. The TCP is located in this layer and the IP protocol is in layer 3 1 . As you can see, all of this is at a very low level since HTTP is at layer 7, the application layer, well above the sockets.
Basically you can't compare HTTP requests with sockets since the former is a specialization of the latter.
As for HTTP and Websocket requests, both can be made using the HTTP protocol, but they are different protocols 2 .
One of the reasons this was chosen is because proxies normally block anything that is not transmitted on port 80, the default port used by HTTP. When the client (or browser usually) detects that there is a proxy it uses the HTTP CONNECT method to create a persistent connection, otherwise it uses
ws://
orwss://
to connect to the server. Connections withws://
andwss://
are established on port 80 and 443 respectively, exactly the same port as thehttp://
andhttps://
Websockets are necessary since the http protocol was designed from the beginning not to preserve state between requests (it is stateless). What does this mean? That when you make a request to a server and immediately send another, it is not able to realize that the two are related. Cookies, javascript and code are normally used on the server so that it understands what information is related, but the http protocol itself has nothing to do with it. It just dictates that each request be self-sufficient so that the server can respond to it, only identifying each "request/response" pair. This is one of the strongest points of the protocol since it has allowed the creation of multiple forms of client-server communication such as REST and SOAP .very different from each other.
Some types of communication, such as a chat, require that the source is always the same and it is possible to know, for example, if the connection was lost or its status changed. Looking at the previous conditions of the protocol, it is a bit complicated to achieve it (although not impossible) and hence the need for a protocol such as websockets to be able to establish a stable communication tunnel between two parties. Keep in mind that in order for communication to be established , both parties must support websockets , so most frameworks today allow HTTP requests to fallback if websocket is not supported.
Some ways to replace websockets with HTTP requests include
Server Sent Events : Basically an http request that does not finish and in which both parties send information (since the response part only ends when the connection is closed).
Long Pooling : Technique in which requests/responses are constantly sent at regular intervals so that both parties update the state of the connection and pass messages to each other. If one of the parties stops responding, the connection is considered lost.
Comet : Set of techniques that allow a stable connection to be maintained using PUSH technology and other http techniques.
Also read What are long polling websockets server sent events sse and comet
Some frameworks that implement websockets:
Note: The form of the network protocol can be so varied that the IP over carrier pigeons was invented, which is defined in rfc2549 :P