Skip to content

What is WebSocket

Key idea:

WebSocket (RFC 6455) — a protocol on top of TCP providing full-duplex bidirectional communication between browser and server. Unlike HTTP, the connection stays open; the server can push without a request. Use-cases: real-time chat, live notifications, multiplayer games, collaborative editing. Handshake via HTTP Upgrade header → switch to ws:// or wss://.

Below: details, example, related terms, FAQ.

Details

  • Handshake: HTTP GET with Upgrade: websocket + Connection: Upgrade
  • Protocol: ws:// (plaintext), wss:// (TLS, mandatory in production)
  • Frames: binary or UTF-8 text. Max frame 2^63 bytes (rarely hit)
  • Heartbeat: ping/pong frames for keep-alive
  • Alternative: Server-Sent Events (SSE) for server→client only

Example

const ws = new WebSocket('wss://example.com/socket');
ws.onmessage = e => console.log(e.data);
ws.onopen = () => ws.send(JSON.stringify({ type: 'subscribe' }));

Related Terms

Learn more

Frequently Asked Questions

WebSocket or SSE?

WebSocket — bidirectional (both send). SSE — one-way (server→client only). SSE is simpler and works over HTTP, WebSocket is faster.

How to handle disconnects?

Implement reconnect with exponential backoff. Socket.IO does this automatically.

Scaling?

One Node.js handles 10k+ concurrent WebSockets. For 100k+ — Redis pub/sub between instances or a managed service (Ably, Pusher).