WebSockets enable real-time features by upgrading a normal HTTP connection to a persistent, two-way communication channel. Initially, the client sends an HTTP request. The server responds with a status code 101 to switch protocols, establishing a WebSocket connection. This connection remains open, allowing both client and server to send messages anytime without waiting for a response. In NestJS, this is handled by WebSocket gateways and message handlers. The example code shows a server listening for 'message' events and responding immediately. The execution table traces each step: connection upgrade, message sent by client, server processing, and response sent back. Variables like connection state and data sent change accordingly. This persistent connection is why WebSockets are ideal for real-time apps like chats or live feeds. The connection only closes when either side decides to end it, keeping communication fast and continuous.