About

What Is WebSocket? Two-Way Messaging Between Browser and Server

Keeps the TCP connection open after the HTTP handshake (RFC 6455)

WebSocket is a protocol defined by RFC 6455 that maintains a TCP connection after the initial HTTP handshake to support bidirectional communication. The server can send data to the client first, making it ideal for real-time applications.

Architecture Diagram

🌐
Client
Browser / Bot / App
① HTTP Upgrade →
← ② 101 Switching
TCP Keep-Alive
← ③ Bidirectional Messages →
④ Close Frame
🖥️
Server
WebSocket Server
Flow Description
  1. Client initiates handshake with HTTP Upgrade request
  2. Server responds with 101 Switching Protocols, TCP connection maintained
  3. Bidirectional channel — both server and client send messages freely
  4. Either side can close connection with Close frame
Use cases: Slack Socket Mode, Discord Gateway, real-time chat, games

How It Works

1

Client initiates WebSocket handshake with HTTP Upgrade request

2

Server responds with 101 Switching Protocols, maintaining TCP connection

3

Bidirectional channel established — both server and client can freely send messages

4

Either side sends a Close frame to terminate the connection

Pros

  • ✓ True bidirectional real-time communication
  • ✓ Server can push data first
  • ✓ Lightweight frames without HTTP overhead
  • ✓ Works behind firewalls via outbound connections

Cons

  • ✗ Connection maintenance cost (server memory/connections)
  • ✗ Reconnection logic needed on disconnect
  • ✗ Complex load balancer configuration
  • ✗ No HTTP caching

Use Cases

Chat applications Real-time dashboards/monitoring Online games Collaborative editors (Google Docs) Real-time speech-to-text (STT — Deepgram, Azure Speech) Live streaming comments/reactions
← What Is the Client-Server Model? The Basics of Request and Response What Is SSE? A Simpler One-Way Stream Than WebSocket →