Skip to main content
Practice Problems

What is WebSocket and how does it work?

WebSocket is a computer communications protocol providing full-duplex communication channels over a single TCP connection. Used for interactive communication between client and server.

How WebSocket Works

Client initiates upgrade from HTTP to WebSocket, then connection stays open for bidirectional communication.

Real-World Example

```javascript // Client const socket = new WebSocket('ws://localhost:8080');

socket.addEventListener('open', () => { console.log('Connected'); socket.send('Hello Server!'); });

socket.addEventListener('message', (event) => { console.log('Message:', event.data); }); ```

Used for: chat applications, live updates, gaming, collaborative tools, real-time dashboards.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems