HTML and CSS Reference
So, where does this bring us? To eliminate many of these issues, the Connectivity section
of the HTML5 specification includes WebSocket. WebSocket is a naturally full-duplex,
bidirectional, single-socket connection. With WebSocket, your HTTP request becomes a
single request to open a WebSocket connection (either WebSocket or WebSocket over TLS
(Transport Layer Security, formerly known as SSL)), and reuses the same connection
from the client to the server, and the server to the client.
WebSocket reduces latency because once the WebSocket connection is established,
the server can send messages as they become available. For example, unlike polling,
WebSocket makes a single request. The server does not need to wait for a request from
the client. Similarly, the client can send messages to the server at any time. This single
request greatly reduces latency over polling, which sends a request at intervals, regardless
of whether messages are available.
Figure 1-3 compares a sample polling scenario with a WebSocket scenario.
Figure 1-3. Polling vs WebSocket
In essence, WebSocket fits into the HTML5 paradigm of semantics and
simplification. It not only eliminates the need for complicated workarounds and latency
but also simplifies the architecture. Let's delve into the reasons a bit further.
Why Do You Need WebSocket?
Now that we've explored the history that brought us to WebSocket, let's look at some of
the reasons why you should use WebSocket.