๐Ÿ”Œ

WebSocket

Bidirectional Real-time Communication

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