WebSocket is a communication protocol through which we are able to connect between a client and a server. In WebSocket, unlike HTTP, data exchange occurs continuously during the connection and both the client and server can send or receive data. WebSocket is useful when data is needed in real time and we can't afford to constantly query the server. The WebSocket protocol is most often chosen for solutions like Chat, Notifications, or Data Streaming.
WebSocket vs. HTTP differences
The principle of HTTP is to continuously query a given source and then receive a response. Each request consists of opening a connection to the server, a request and a response. In the case of a WebSocket request, we are dealing with a request over HTTP handshake protocol, and then opening a persistent connection over TCP/IP protocol and full two-way communication called full-duplex.
Advantages of WebSocket
- WebSocket is a protocol that is ideal for real-time communication. Unlike HTTP, it does not require constant querying for changes, but instead listens for them from the server side.
- It reduces response times by maintaining a persistent TCP connection, the whole request is lighter than HTTP so the number of KB for multiple requests is lower
Disadvantages of WebSocket
- Broken connection should be refreshed. Therefore, in case of poor Internet connection, you need to take this into account
- Older browsers may not support the WebSocket protocol
- More difficult to configure WSS type connections
Why choose the WebSocket protocol?