WebSocket surfaced in the era of HTTP/1.1. The basic idea is to enable bi-directional communication between the client and the server under a single TCP connection. Instead of relying on the traditional request-response model, under WebSocket both parties can send messages freely.
- currencies exchange
- live feed of a sports event
After the “normal” TCP connection gets established, the client has to send a GET (UPGRADE) request that asks the server to switch to WebSocket. The server responds with 101 (Switching Protocols). After that, both parties can communicate via WebSocket.
Here’s an example of how the upgrade looks like in the HTTP protocol (taken from the Wikipedia):
Client request (just like in HTTP, each line ends with
\r\n and there must be
an extra blank line at the end):