HTTP(Refer article:Where is HTTP3.0 stronger than HTTP2.0) is based on the TCP protocol. At the same time, only one of the client and the server can actively send data, which is half-duplex communication.
Usually, when we open a certain webpage, every time we click an option on the webpage, the front end will send an HTTP request, and the website will return an HTTP response. This method of actively requesting by the client and responding by theserver satisfies the functional scenarios of most web pages. But in this case, the server will not take the initiative to send messages to the client. In scenarios like web games, the client and the server need to actively send a large amount of data to each other.
Therefore, we need a new protocol based on TCP, the new application layer protocol WebSocket.
What is WebSocket?
WebSocket is a network protocol for full-duplex communication over a single TCP connection. It uses an HTTP-like handshake to establish a connection, and then uses a separate persistent connection to communicate. This approach enables WebSocket to communicate in real time between the browser and the server without sending HTTP requests multiple times to obtain data. This means that the connection is persistent, it remains open until one party actively closes the connection. This makes WebSocket very suitable for real-time applications, because it does not need to constantly establish and close the connection.
How does WebSocket establish a connection?
A WebSocket connection is usually established between a client (such as a browser) and a server. The client sends an HTTP request to establish a connection, and the server returns an acknowledgment that the connection has been established. Afterwards, the client and server can communicate bidirectionally over this connection. Clients can send messages to servers, and servers can send messages to clients. Messages can be arbitrary byte arrays and can be encoded in any format.
WebSocket is an essential client-server communication tool, and its utility needs to be fully understood and usage scenarios avoided in order to benefit from its maximum potential.
Practical uses of WebSockets
The most common use is in real-time application development, where it facilitates persistent display of data on the client side. Since the backend server is constantly sending this data back, WebSocket allows this data to be pushed or transferred without interruption within an open connection, making such data transfers faster and taking advantage of the performance of the application.
Chat application developers call WebSocket in one-off exchanges, publishing, broadcasting messages, etc. Communication is simple, lightweight and fast since the same WebSocket connection is used to send or receive messages.
While developing a game application, the server must receive data continuously without requiring a refresh of the UI. WebSocket achieves this without affecting the game application UI.
Since both HTTP and WebSocket are used for application communication, people are often confused and it is difficult to choose one from the other.
Choice between WebSocket and HTTP
WebSocket is a framework bidirectional protocol. In contrast, HTTP is a one-way protocol that works on top of the TCP protocol. Because the WebSocket protocol can support uninterrupted data transmission, it is mainly used for real-time application development. HTTP is stateless and used to develop RESTful and SOAP applications. SOAP can still be implemented using HTTP, but REST is widely spread and used.
In WebSocket, the communication happens on both ends, which makes it a faster protocol. In HTTP, the connection is established at one end, which makes it a bit slower than WebSocket. WebSocket uses a unified TCP connection that requires one party to terminate the connection. Until it happens, the connection stays alive. HTTP requires separate connections for separate requests. After the request is completed, the connection is automatically disconnected.
HTTP long polling
There is also more demand for network data security today, and the WebSocket connection uses a standard TCP connection, so it can be used on any network that supports TCP. It uses the ws:// and wss:// protocol prefixes, which can be combined with any domain name or IP address.
WebSocket connections can be encrypted, and TLS (Transport Layer Security) protocol can be used to protect data security. This encrypted connection uses the wss:// protocol prefix, as opposed to the ws:// protocol prefix used by unencrypted connections. In addition, WebSocket also supports the Secure Handshake Protocol (Secure Handshake Protocol), which makes it possible to encrypt communication between the browser and the server. In general, WebSocket has high security and can protect data security.
In addition, it should be noted that WebSocket is not a new protocol based on HTTP, because WebSocket only uses HTTP when establishing a connection, and it no longer has any relationship with HTTP after the upgrade is completed.