How it works
Unlike HTTP, where each request opens and closes a connection, WebSocket establishes a persistent connection after an initial handshake. After that, client and server can send messages at any time, in both directions, with low latency. The connection stays open until one of the parties closes it.
Practical example
A live support chat uses WebSocket: when the customer types, the message reaches the agent in milliseconds, without needing to reload the page or do polling. Streaming AI applications also use it to deliver tokens as they are generated.
What it's for
It's the natural choice for real-time applications: chats, live dashboards, games and AI interfaces that display responses token by token. It avoids the cost of opening many short HTTP connections and reduces perceived latency for the user.
When not to use
For one-off and sporadic requests, HTTP remains simpler and more appropriate. WebSocket adds connection management complexity and can be overkill when continuous communication is not needed.