Following are some of the drawbacks of HTTP due to which they are unsuitable for certain scenarios-
- Traditional HTTP requests are unidirectional – In traditional client server communication, the client always initiates the request.
- Half Duplex – User requests for a resource and the server then serves it to the client. The response is only sent after the request. So at a time only a single request occurs.
- Multiple TCP connections – For each request a new TCPsession is needed to be established and then closed after receiving the response. So without using WebSockets we will have multiple sessions.
- Heavy – Normal HTTP request and response require exchange of extra data between client and server.
WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.
Websocket is used to develop two way communication between server and browser. For example, in a chat application, if a user sends a message to a different user, the the application sends that message to the server first and the server informs the application of the second user . If we don’t use websocket, the application or the client will have to pull the server data continuously for any new updates.
In Sprint Boot, we can easily create a websocket application. In this tutorial, I will show you how to create a websocket server in Spring boot. We will learn how to connect and disconnect from a socket server and how to send a simple text message. The client will subscribe to the server, it will accept a string and push a separate string to the subscriber client.
- WebSocket are bi-directional – Using WebSocket either client or server can initiate sending a message.
- WebSocket are Full Duplex – The client and server communication is independent of each other.
- Single TCP connection – The initial connection is using HTTP, then this connection gets upgraded to a socket based connection. This single connection is then used for all the future communication
- Light – The WebSocket message data exchange is much lighter compared to http.