Now obviously creating new TCP connection for every request is not very performant and HTTP has not been unaware of this. In fact, as part of HTTP/1.1, persistent connections were introduced to alleviate this shortcoming of HTTP. In case of WebSocket, the client initiates a Protocol Handshake request in HTTP and then waits until the server responds accepting an upgrade to WebSocket from HTTP.
But suppose you have hundreds or thousands of clients (hundreds of thousands, in the case of BBC) hammering the server with requests that yield nothing new between updates. Now, you could code the client to make HTTP requests at a frequent interval just in case something happens and, for a handful of clients, that might work well enough. When loading a web page or downloading a file, this overhead is negligible. However, it could have a noticeable impact on your app’s performance if you’re sending high-frequency requests with small payloads. This stateless design is advantageous because it makes it possible to deploy additional servers to handle requests without the need to synchronize state logic between servers.
Realtime updates with WebSockets
The authorized client creates a request, the recipient server accepts it, interprets it, and provides an appropriate response. With WebSocket, working over persistent TCP communication, it’s possible for server and client both to send data independent of each other, and in fact, to many communicating parties! For instance, in HTTP, usually the client sends that request, and then the server responds with requested data. There is no generic way for the server to communicate with the client on its own. Of course, patterns and solutions have been devised to circumvent this like Server-Sent Events (SSE), but these were not completely natural. Of course, since Protocol Handshake happens over HTTP, it follows the sequence from the previous diagram.
HTTP/2 introduced several features such as multiplexing, header compression, and unlimited connections per domain. In doing so, HTTP/2 makes HTTP streaming and SSE (a standardized implementation of HTTP streaming) a viable alternative to WebSockets when you need realtime updates. With that said, WebSockets are currently the only way to achieve full-duplex bidirectional communication in the browser. what is websocket used for Unlike HTTP with its request-response model, WebSockets are specifically designed to enable realtime bidirectional communication between the server and client. Feathers uses this for exposing its APIs both ways, via a traditional HTTP REST API and completely through websockets in which case it also sends real-time updates. Proxies are compatible with almost all types of communication protocols.
Comparing HTTP vs WebSockets
I’ve wrote more code in Websockets than I ever wrote with SocketIO in the past, for ten times simpler things than I did with SocketIO. HTTP and WebSocket both are communication protocols used in client-server communication. Dedicated to API communication, this protocol enabled bi-directional communication. Build over TCP, WebSocket makes real-time data push to a client.