WebSockets provide full-duplex communication between client and server over a single TCP connection. Socket.io adds reliability features on top of raw WebSockets.
Server setup: const io = new Server(httpServer). Handle connections: io.on('connection', (socket) => {}). Emit events: socket.emit('message', data). Listen: socket.on('message', handler).
Rooms group sockets: socket.join('room1'). Broadcast to room: io.to('room1').emit('event'). Namespaces separate concerns: const chat = io.of('/chat').
Socket.io automatically handles reconnection, fallback to long-polling, and connection state recovery. Use with Redis adapter for horizontal scaling across multiple server instances.
Use cases: chat applications, real-time dashboards, multiplayer games, collaborative editing, live notifications, and IoT device communication.
Always validate and sanitize incoming socket data. Rate limit events to prevent abuse. Use JWT authentication on connection handshake.