A WebSocket is a communication protocol that provides a full-duplex (two-way) channel over a single connection between a client (like a browser) and a server. It enables real-time, interactive communication, making it ideal for applications where frequent and low-latency updates are necessary. Unlike traditional HTTP, which follows a request-response model where the client has to request data each time, WebSockets allow for continuous communication once the connection is established. Both the client and server can send and receive messages independently at any time during the connection.
The communication starts with a standard HTTP request from the client to initiate the WebSocket connection. This is called the handshake.
If the server accepts the handshake, the protocol is upgraded to WebSocket, and the connection becomes persistent.
Once connected, both the client and server can freely send and receive messages as events.