High level flow which shows the difference between XHR (using HTTP) and a Websocket client – server interaction.
Websockets were designed as a low latency protocol to lower the overhead during client and server exchanges. They provide a valuable advantage because they work over TCP port 80 and TCP port 443 (when using TLS), the same ports as HTTP/HTTPS.
The websocket schemes are ws or wss (using TLS). It not only allows traditional web objects (JSON or XML for example) but binary data as well, which could present great advantages.
When to use Websockets ?
By introducing a persistent connection between client and server, websockets provide a helpful way of handling specific business cases where the traditional HTTP is not able to handle efficiently : like real-time communications, low-latency interactions. Here are a few examples:
- Gambling: key interactions between client and server
- Real-time sport updates: end-users want to get the latest update for an event (football game for example) as quickly as possible
Instant messaging and collaborative work (editing/coding): teams are now working in a distributing mode to get the informations in real-time.
- Trading: many websites deliver trading updates via tickers, and when each millisecond counts, delays can have a big impact