WebSocket

Definition

Communication protocol providing full-duplex communication over a single TCP connection, ideal for real-time applications like chat and gaming.

Use Cases

Provider Equivalents

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