phoenix24
phoenix24

Reputation: 2102

Why do we need web-sockets?

This is more of a n00b question, but I've never really known the answer.

so why do we need the websockets protocol?

and, what are the advantages over comet-style/long poll/hanging GET-style use of HTTP?

Upvotes: 28

Views: 11948

Answers (4)

ShahRokh
ShahRokh

Reputation: 1025

1-WebSocket is a naturally full-duplex, bidirectional, single-socket connection. With WebSocket, your HTTP request becomes a single request to open a WebSocket connection and reuses the same connection from the client to the server, and the server to the client.

2-WebSocket reduces latency. 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.

3-WebSocket makes real-time communication much more efficient. You can always use polling (and sometimes even streaming) over HTTP to receive notifications over HTTP. However, WebSocket saves bandwidth, CPU power, and latency. WebSocket is an innovation in performance.

4-WebSocket is an underlying network protocol that enables you to build other standard protocols on top of it.

5-WebSocket is part of an effort to provide advanced capabilities to HTML5 applications in order to compete with other platforms.

6-WebSocket is about Simplicity

Upvotes: 15

Ahmet Akyol
Ahmet Akyol

Reputation: 207

Here's an article about benefits of websocket over polling at websocket.org

Upvotes: 2

Daniel Earwicker
Daniel Earwicker

Reputation: 116654

It isn't clear that we do need them. In the scenario of pushing events to the client, a page can make ordinary AJAX GET requests in a loop, and the server can "hang" until events are available. After some timeout passes the server can return a "no events" response so the client will reconnect. During the period where the connection is open and the client is waiting for a response, there is an effective push channel from the server back to the client.

The timeout period can be adjusted to reduce the amount of unnecessary reconnecting, though it cannot typically be infinite because most server frameworks will terminate the server-side process if it appears to have hung for too long.

Given this existing capability, the question is: does a new communication framework really add significant value over what can already be done? It wouldn't really enable something that cannot be done. It would only slightly improve it.

Upvotes: 1

Spooks
Spooks

Reputation: 7177

Comet and Ajax can both deliver end-user experiences that provide desktop-like functionality and low user-perceived latency, only Web Sockets lives up to the promise of providing a native means to accurately and efficiently stream events to and from the browser with negligible latency.

With polling it makes unnecessary requests and, as a result, many connections are opened and closed needlessly in low-message-rate situations.(as with polling it sends HTTP requests at regular intervals and immediately receives a response.)

Web Sockets remove the overhead and dramatically reduce complexity.

Upvotes: 20

Related Questions