WebSocket
ELI5 — The Vibe Check
WebSocket is like upgrading a walkie-talkie from push-to-talk to a full phone call. Normal HTTP is push-to-talk (you ask, server responds, connection closes). WebSocket keeps the line open so both sides can talk anytime. Perfect for chat apps, live dashboards, and games.
Real Talk
WebSocket is a full-duplex, bidirectional communication protocol over a single, persistent TCP connection. It starts with an HTTP handshake (upgrade request) then switches protocols. Unlike HTTP, either side can send data at any time without a new request.
Show Me The Code
// WebSocket client
const ws = new WebSocket('wss://example.com/socket');
ws.onopen = () => ws.send('Hello Server!');
ws.onmessage = (event) => console.log('Received:', event.data);
// WebSocket server (Node.js + ws library)
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
ws.on('message', (msg) => ws.send(`Echo: ${msg}`));
});
When You'll Hear This
"Use WebSockets for real-time chat instead of polling." / "The live dashboard uses WebSocket to push updates without page refresh."
Related Terms
HTTP (HyperText Transfer Protocol)
HTTP is the language your browser uses to ask websites for stuff. You type a URL, your browser shouts 'hey, give me that page!
HTTP/2
HTTP/2 is a supercharged version of HTTP. With HTTP/1.1, you could only ask for one thing at a time per connection.
Protocol
A protocol is just an agreed set of rules for how two parties communicate.
Socket
A socket is the combination of an IP address plus a port number — it's the complete 'address' for a specific connection.
TCP (Transmission Control Protocol)
TCP is like sending a package with delivery confirmation.