WebSockets are a communication protocol that allows for real-time, full-duplex communication between a client (like a web browser) and a server over a single, long-lived connection. Unlike traditional HTTP requests where the client initiates communication and the server responds, WebSockets enable both the client and server to send messages to each other at any time without the overhead of multiple HTTP requests.
How it works:
-
Handshake: The client sends a WebSocket handshake request to the server, indicating its desire to establish a WebSocket connection.
Client:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Version: 13
-
Connection Establishment: If the server supports WebSockets, it responds with a WebSocket handshake indicating the connection is established.
Server:
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
-
Bi-Directional Communication: Once the connection is established, both client and server can send messages to each other directly without waiting for a request.
Client to Server:
WebSocket.send("Hello Server!");
Server to Client:
WebSocket.onmessage = function(event) { console.log("Message from server: " + event.data); };
Use Cases:
- Real-time chat applications
- Multiplayer online games
- Live collaboration tools
- Stock market tickers
- Any application requiring real-time updates