WebSocket
Definition
Communication protocol providing full-duplex communication over a single TCP connection, ideal for real-time applications like chat and gaming.
Use Cases
- Slack: Real-time updates for messages, typing indicators, and presence in team channels — Slack clients maintain persistent connections to receive events as they happen, avoiding constant polling for new messages (Near-instant message delivery and a responsive collaboration experience at large scale)
- Trello: Live collaboration where board changes (cards moved/edited) appear immediately for all viewers — Clients keep an open, bidirectional connection to receive updates when other users make changes (Reduced need for page refreshes and smoother multi-user collaboration)
- Coinbase: Streaming live market data (price ticks and order book updates) to trading dashboards — Provides WebSocket-based market data feeds so clients can subscribe to channels and receive continuous updates (Lower-latency data delivery compared to repeated HTTP requests, improving real-time trading and monitoring)
Provider Equivalents
- AWS: Amazon API Gateway (WebSocket APIs)
- Azure: Azure Web PubSub
- GCP: Google Cloud Run (WebSocket support) / Google Cloud Load Balancing (WebSocket support)
- OCI: OCI Load Balancer (WebSocket support)
Frequently Asked Questions
- What's the difference between WebSocket and HTTP long polling?
- WebSocket keeps one persistent connection open so the server can push updates to the client instantly and the client can send messages back at any time. HTTP long polling repeatedly opens requests: the client asks for updates, the server holds the request until it has data (or times out), then the client immediately makes another request. Long polling is simpler to fit into traditional HTTP infrastructure but creates more overhead and usually higher latency than WebSockets for frequent real-time updates.
- When should I use WebSocket?
- Use WebSocket when you need real-time, two-way communication with frequent updates, such as chat, multiplayer games, collaborative editing, live dashboards, notifications, or streaming market/IoT events. Avoid WebSocket if updates are infrequent (simple REST polling may be enough), if you only need one-way server-to-client updates (Server-Sent Events may be simpler), or if your environment makes long-lived connections difficult (some proxies, strict corporate networks, or very short-lived serverless runtimes without connection support).
- How much does WebSocket cost?
- The WebSocket protocol itself is free, but running it costs money through the infrastructure that maintains persistent connections and transfers data. Key cost drivers are: (1) connection minutes/hours (keeping sockets open), (2) number of messages and total data transferred (egress is often a major factor), (3) compute/memory needed to handle concurrent connections, and (4) any managed gateway or real-time messaging service charges. For example, AWS API Gateway WebSocket APIs charge for messages and connection minutes; Azure Web PubSub charges based on units/messages/connection time; if you self-host on VMs/containers, you pay for compute plus load balancer and network egress.
Category: networking
Difficulty: intermediate
See Also